• 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      |         |               |
    +---------------------+------------+---------------------------------+--------------+---------------+-----------+-------------+----------+-----
  • 相关阅读:
    postgresql允许远程访问的配置修改
    Oracle常用监控sql语句
    Python Twisted 学习系列22(转载stulife最棒的Twisted入门教程)
    Python Twisted 学习系列21(转载stulife最棒的Twisted入门教程)
    有趣的题目
    入学测试题详解
    完成这个例子,说出java中针对异常的处理机制。
    遍历Map key-value的两种方法
    java中的 FileWriter类 和 FileReader类
    Java中Split函数的用法技巧
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/5457838.html
Copyright © 2020-2023  润新知