zoukankan      html  css  js  c++  java
  • 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      |         |               |
    +---------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+-----
  • 相关阅读:
    koa 放置 前台打包dist 目录
    tomcat startup.bat 包含springboot的输出 里面乱码的解决方案
    base64 转文件上传
    4时4态 加被动 例句:I will have been being done
    软件推荐 Notable / 现改用 Vnote 了
    [win10] 开始-设置 / 右键-显示设置 / 右键个性化 等都不好使了。。 ms-settings:display
    viewui tree 自定义化(源码copy出来改动)#添加 获取selected 解决方案
    idea 暂存 Stash Changes Git/Repository/Stash Changes 恢复暂存 UnStash Changes
    vm 虚拟机总是蓝屏 移除打印机和声卡 移除这俩硬件 (大文件用飞秋传输)
    docker中mysql 汉字乱码,显示问号
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/5457838.html
Copyright © 2011-2022 走看看