如果我有:
class Foo < ActiveRecord::Base
belongs_to :belongable, :polymorphic => true
end
class Bar < SuperBar
end
class SuperBar < ActiveRecord::Base
end
然后执行:
f = Foo.new :belongable => Bar.first
f.save
f.belongable_type # => SuperBar
f.belongable.type # => Bar
f.belongable.type.class.model_name
使用STI时,ActiveRecord似乎将belongsable_type列设置为可归属的ActiveRecord :: Base的第一个子类。有没有一种方法可以用来获得它?
答案 0 :(得分:0)
看起来像belongable_type.class.base_class
这样做了。见https://github.com/rails/rails/blob/08754f12e65a9ec79633a605e986d0f1ffa4b251/activerecord/lib/active_record/inheritance.rb#L100