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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值