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 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的更多相关文章
- Rails5 Model Document
创建: 2017/06/09 更新: 2017/06/21 更新: 2017/06/23 对待未完成的追加# TODO: 更新: 2017/06/29 修正文件名db/seed.rb ---> ...
- MySQL 优化之 ICP (index condition pushdown:索引条件下推)
ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行 ...
- 在v-for中利用index来对第一项添加class(vue2.0)
<li v-for="(el,index) in event" v-bind:class="{ 'm-swipe-active': !index}"> ...
- Ubuntu-server 下Apache2 配置.htaccess 隐藏thinkPHP项目index.php
需要开启Apache2的rewrite模块 1.打开/etc/apache2/apache2.conf 将文件中的AllowOverride None改为AllowOverride All 2.修改m ...
- SQL Server-聚焦强制索引查询条件和Columnstore Index(九)
前言 本节我们再来穿插讲讲索引知识,后续再讲数据类型中的日期类型,简短的内容,深入的理解,Always to review the basics. 强制索引查询条件 前面我们也讲了一点强制索引查询的知 ...
- directx12中vetex buffer、index buffer和constant buffer绑定piple line的时机
类别 时机 函数 建Heap vetex buffer 在Draw函数中 ID3D12GraphicsCommandList::IASetVertexBuffer 否 index buffer 在Dr ...
- URL_MODEL 2 不能访问 在APACHE服务器上的访问方式上去除index.php
thinkphp URL_MODEL=2,访问链接http://i.cnblogs.com/Online/index.html 报错: Not Found The requested URL /on ...
- index+match函数在压实度中对盒号盒质量随机不重复的最佳使用
首先按照升序排列好盒号和盒质量,使其一一对应, 盒号 盒重量 随机值rand() 随机值大小排列rank 1 2001 0.01 ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- Windows Phone8 中如何引用 SQLite 数据库
SQLite数据库介绍 1.SQLite是一款轻型的嵌入式数据库,使用C++开发,使用非常广泛 2.SQLite是一款跨平台的数据库,支持Windows.Linux.Android.IOS.Windo ...
- [CH#58解题报告]
题目:http://206.contesthunter.org/contest/CH%20Round%20%2358%20-%20OrzCC%E6%9D%AFnoip%E6%A8%A1%E6%8B%9 ...
- ThinkPHP之MVC与URL访问
一.初探 我们在apache的www目录下创建一个文件夹,其命名为我们的应用名.然后通过入口文件生成我们的应用. 当我们用ThinkPHP创建好一个应用后,其目录结果如下所示 那么我们如何来访问我们应 ...
- 年前辞职-WCF入门(6)
前言 昨天早上去医院做入职体检,被告知要预约,本以为是要排队,我连视频都准备好了...结果就回来了.下午去了新公司那边找房子,2了,因为公司提供了班车列表,我既然就只在班车所经过的几个地方找,却遗漏了 ...
- Chrome的Crash Report服务
<本文转自:http://www.cppblog.com/woaidongmao/archive/2009/10/22/99211.aspx> 本文翻译自debugInfo网站上一篇文章g ...
- Future模式
Future模式简介 Future模式有点类似于网上购物,在你购买商品,订单生效之后,你可以去做自己的事情,等待商家通过快递给你送货上门.Future模式就是,当某一程序提交请求,期望得到一个答复.但 ...
- js实现开灯关灯效果
<!DOCTYPE html> <html> <body> <script> function changeImage() { element=docu ...
- shell 命令遇到的一些问题
1. command not found 一般都是未安装,需要root 权限去安装服务,就可正常使用.比如rz, sz, crontab, sendemail, lftp等 2. rz 传输失败,输 ...
- mysql中的having
from子句后面可以用where选择行,group by子句后面可以用having子句选择行, having中条件的定义和where中很相似,但是having中可以直接用聚合函数,但是where中不能 ...
- Notions of Flow Networks and Flows
这篇随笔是对算法导论(Introduction to Algorithms, 3rd. Ed.)第26章 Maximum Flow的摘录. ------------------------------ ...