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
- index and polymorphic
- Table and Index rebuild
- Table and Index Movement
- Index and Statistics
- max value and index
- DML command and Index
- mysql index and key
- recoll index and usage
- MySql key and index
- elasticsearch index and store
- Polymorphic Viruses
- Polymorphic Protector
- 多态(polymorphic)
- has_many polymorphic
- polymorphic read
- Clustered Index and Non-Clustered Index
- Bitmap index AND bitmap join index
- local index and global index [转]
- Activity启动模式详解
- PhotoShop CS6 官方简体中文正式版原版+破解补丁下载
- twitter snow flake 雪花算法
- mybatis中#{}和 ${}区别
- MYSQL必知必会读书笔记 第一章
- index and polymorphic
- class.isAssignableFrom
- MongoDb的普通查询操作
- LeetCode 263. Ugly Number
- ANMPP 12已经发布,安卓上的php环境 - Android NGINX MYSQL PGSQL PHP-FPM
- 下载APk,并在通知栏显示下载进度(两种方式)
- solr查询语法
- Android第三方acharrtengine绘制折线图
- 5.execve()到底干了啥?