index and polymorphic

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
  end
end

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
  end
end


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)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值