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 tv_subchannel_items;
+---------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tv_subchannel_items | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| tv_subchannel_items | 1 | subchannel_items_showable_index | 1 | showable_type | A | 0 | NULL | NULL | YES | BTREE | | |
| tv_subchannel_items | 1 | subchannel_items_showable_index | 2 | showable_id | A | 0 | NULL | NULL | YES | BTREE | | |
| tv_subchannel_items | 1 | subchannel_items_sort_index | 1 | subchannel_id | A | 0 | NULL | NULL | YES | BTREE | | |
| tv_subchannel_items | 1 | subchannel_items_sort_index | 2 | state | A | 0 | NULL | NULL | YES | BTREE | | |
| tv_subchannel_items | 1 | subchannel_items_sort_index | 3 | version | A | 0 | NULL | NULL | YES | BTREE | | |
| tv_subchannel_items | 1 | subchannel_items_sort_index | 4 | position | A | 0 | NULL | NULL | YES | BTREE | | |
+---------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+-----

index and polymorphic的更多相关文章

  1. Rails5 Model Document

    创建: 2017/06/09 更新: 2017/06/21 更新: 2017/06/23 对待未完成的追加# TODO: 更新: 2017/06/29 修正文件名db/seed.rb ---> ...

  2. MySQL 优化之 ICP (index condition pushdown:索引条件下推)

    ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行 ...

  3. 在v-for中利用index来对第一项添加class(vue2.0)

    <li v-for="(el,index) in event" v-bind:class="{ 'm-swipe-active': !index}"> ...

  4. Ubuntu-server 下Apache2 配置.htaccess 隐藏thinkPHP项目index.php

    需要开启Apache2的rewrite模块 1.打开/etc/apache2/apache2.conf 将文件中的AllowOverride None改为AllowOverride All 2.修改m ...

  5. SQL Server-聚焦强制索引查询条件和Columnstore Index(九)

    前言 本节我们再来穿插讲讲索引知识,后续再讲数据类型中的日期类型,简短的内容,深入的理解,Always to review the basics. 强制索引查询条件 前面我们也讲了一点强制索引查询的知 ...

  6. directx12中vetex buffer、index buffer和constant buffer绑定piple line的时机

    类别 时机 函数 建Heap vetex buffer 在Draw函数中 ID3D12GraphicsCommandList::IASetVertexBuffer 否 index buffer 在Dr ...

  7. URL_MODEL 2 不能访问 在APACHE服务器上的访问方式上去除index.php

    thinkphp URL_MODEL=2,访问链接http://i.cnblogs.com/Online/index.html  报错: Not Found The requested URL /on ...

  8. index+match函数在压实度中对盒号盒质量随机不重复的最佳使用

    首先按照升序排列好盒号和盒质量,使其一一对应, 盒号    盒重量    随机值rand()     随机值大小排列rank 1         2001       0.01             ...

  9. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

随机推荐

  1. 微信小程序内测申请

    想申请微信小程序的内测?别做梦了! 小程序内测是邀请制的,目前就发放了200个内测邀请.正因为稀缺,江湖传言内测资格已经炒到300万(一套房)一个了 但是!!!!你可以先熟悉一下相关资料和文档,下载一 ...

  2. (旧)子数涵数·Flash——路径补间

    一.打开flash软件(图为flash8) 二.创建新项目->Flash文档 三.使用椭圆工具,绘制一个圆形图像(快捷键为O,很形象吧) 四.在后面若干帧中插入关键帧,并移动刚刚绘制好的图像的位 ...

  3. ssh 返回错误 Too many authentic authentication failures for root 的时候检查 ssh 配置

    路径 cd /etc/ssh ls -ltr sudo vi sshd_config 改为以下内容(yes): PermitRootLogin yes

  4. IOS动态判断UITextField是否输入为手机号

    现在使用的app大部分都用到手机号注册,很多app注册的时候会判断手机号,可以根据当前输入文本来判断“获取验证码”的按钮是否可用 判断输入文本是通过UITextField的代理的 -(BOOL)tex ...

  5. linux用终端上传文件和文件家到远程的服务器

    文件上传:scp 本地文件地址 root@aifei8.net:/var/www/html/landrover 文件夹上传:1.先进入文件夹目录 2. 执行命令:scp -r . root@aifei ...

  6. linux 安装webbench

    webbench :1.5  http://soft.vpser.net/test/webbench/webbench-1.5.tar.gz从官网下载webbench-1.5.tar.gz1.解压 t ...

  7. 给java应用打包

    C:\dollapp\classes> jar -cvf C:\dollapp\deploy\dollapp.jar *.* 上面的jar命令会把 C:\dollapp\classes 下的 所 ...

  8. Html-Css-设置DIV边框圆滑

    border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; ...

  9. java将数据写入到txt文件中(txt有固定的格式)

    java将数据写入到txt文件中,这个应该对于学过java I/O的人来说是很简单的事情了,但是如果要将数据以固定的格式写入到txt文件中,就需要一定的技巧了. 这里举个简单的例子,以供参考: 比如我 ...

  10. input type="submit" 和"button"有什么区别?

    http://www.zhihu.com/question/20839977 在一个页面上画一个按钮,有四种办法: <input type="button" /> 这就 ...