开发者

Rails STI and the setting of the "type" string

开发者 https://www.devze.com 2023-02-08 00:15 出处:网络
I think I need to use STI in Rails. Here\'s my class: class Person < ActiveRecord::Base end class Landlord < Person

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 Landlo开发者_如何学Pythonrd 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消