zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Leetcode#117 Populating Next Right Pointers in Each Node II
    Leetcode#123 Best Time to Buy and Sell Stock III
    获取文件大小的方法
    内存映射
    git patch
    git cherry-pick
    关于extern的说明
    Linux如何查看与/dev/input目录下的event对应的设备
    如何在Linux下统计高速网络中的流量
    [: ==: unary operator expected 解决方法
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/5865333.html
Copyright © 2011-2022 走看看