index and polymorphic

来源:互联网 发布:tinyumbrella java 编辑:程序博客网 时间:2024/05/19 05:40

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

class CreateStars < ActiveRecord::Migration  def self.up    create_table :cms_tv_stars do |t|       t.string :username      t.string :image      t.integer :person_id      t.timestamps    end     change_table :cms_tv_stars do |t|       t.index :person_id, uniq: true    end   end   def self.down    drop_table :cms_tv_stars  end end

class CreateSubchannelItems < ActiveRecord::Migration  def self.up    create_table :tv_subchannel_items do |t|       t.string :title      t.string :subtitle      t.string :version      t.string :image      t.references :subchannel      t.references :showable, polymorphic: true      t.integer :state, limit: 1, default: 0      t.integer :position, default: 1      t.timestamps    end     change_table :tv_subchannel_items do |t|       t.index [:showable_type, :showable_id], name: :subchannel_items_showable_index      t.index [:subchannel_id, :state, :version, :position], name: :subchannel_items_sort_index    end   end   def self.down    drop_table :tv_subchannel_items  end end

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

If you have an instance of the Picture model, you can get to its parent via @picture.imageable.

To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:

class CreatePictures < ActiveRecord::Migration  def change    create_table :pictures do |t|      t.string  :name      t.integer :imageable_id      t.string  :imageable_type      t.timestamps null: false    end     add_index :pictures, :imageable_id  endend

This migration can be simplified by using the t.references form:

class CreatePictures < ActiveRecord::Migration  def change    create_table :pictures do |t|      t.string :name      t.references :imageable, polymorphic: true, index: true      t.timestamps null: false    end  endend


Let's check the index in the database

$ bundle exec rails db -p

mysql> show index from cibn_subchannel_items;+-----------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| Table                 | Non_unique | Key_name                        | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |+-----------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| cibn_subchannel_items |          0 | PRIMARY                         |            1 | id            | A         |          17 |     NULL | NULL   |      | BTREE      |         |               || cibn_subchannel_items |          1 | subchannel_items_showable_index |            1 | showable_type | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               || cibn_subchannel_items |          1 | subchannel_items_showable_index |            2 | showable_id   | A         |          17 |     NULL | NULL   | YES  | BTREE      |         |               || cibn_subchannel_items |          1 | subchannel_items_sort_index     |            1 | subchannel_id | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               || cibn_subchannel_items |          1 | subchannel_items_sort_index     |            2 | state         | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               || cibn_subchannel_items |          1 | subchannel_items_sort_index     |            3 | version       | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               || cibn_subchannel_items |          1 | subchannel_items_sort_index     |            4 | position      | A         |          17 |     NULL | NULL   | YES  | BTREE      |         |               |+-----------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+7 rows in set (0.04 sec)





0 0
原创粉丝点击