single-table inheritance 单表继承

type 字段在 Rails 中默认使用来做 STI(single-table inheritance),
当 type 作为普通字段来使用时,可以把SIT的列设置成别的列名(比如不存在的某个列)。

文档在这里
http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-inheritance_column,
使用下面的方法就是设置 STI 使用的列名,默认是‘type',既然不想使用 STI了,可以设置一个不太可能和以后的列名冲突的名字,比如使用 ’_disable‘。

比如可以在 Model 中设置为
self.inheritance_column = '_disable'

Defines the name of the table column which will store the class name on single-table inheritance situations.

The default inheritance column name is type, which means it's a reserved word inside Active Record. To be able to use single-table inheritance with another column name, or to use the column type in your own model for something else, you can set inheritance_column:

self.inheritance_column = 'zoink'


关于single table inheritance, 可以查看下面的文档
http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html




以下是建表语句
class CreateSubjects < ActiveRecord::Migration[5.0]
  def change
    create_table :subjects do |t| 
      t.string :title, limit: 40
      t.string :sub_title, limit: 40
      t.string :categories, limit: 30
      t.string :description, limit: 500 
      t.integer :status, limit: 1
      t.integer :type, limit: 1
      t.integer :use_default_image, limit: 1
      t.integer :has_ranking_list, limit: 1

      t.timestamps
    end
  end 
end
原文地址:https://www.cnblogs.com/iwangzheng/p/5865333.html