Rails:当目标使用STI时,如何在多态关联中获得* _type的值?

时间:2014-08-20 02:44:02

标签: ruby-on-rails activerecord

如果我有:

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的第一个子类。有没有一种方法可以用来获得它?

1 个答案:

答案 0 :(得分:0)