Rails STI and the setting of the “type” string

99封情书 提交于 2019-12-07 06:38:27

问题


I think I need to use STI in Rails.

Here's my class:

class Person < ActiveRecord::Base
end

class Landlord < Person
end

and the people table has a :type column that's a string.

So, what I expect is to see in the table, is that every row that is a Person has the type set to "Person" and every Landlord has the type set to "Landlord". However, that's not what I see. Each Landlord has the type set to "Landlord", but all the Person have their type set to nil. This very well could be the way that rails works, but I was just looking for some confirmation.


回答1:


This is, indeed, how STI works. The base class has a type of NULL in the database (or nil in ruby).




回答2:


You can solve this if you want by adding the type column with a default value of "Person".



来源:https://stackoverflow.com/questions/4858122/rails-sti-and-the-setting-of-the-type-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!