【发布时间】:2010-02-12 15:16:48
【问题描述】:
我有一个 User 模型,它属于 Profile(属于多态)。一个模型有两个子类,但 User 中的 profile_type 总是对应于父模型。
User < ActiveRecord::Base
belongs_to :profile, :polymorphic => true
SomeProf < ActiveRecord::Base
has_one :user, :as => :profile
SomeDeepProf1 < SomeProf
SomeDeepProf2 < SomeProf
然后:
sdp1 = SomeDeepProf1.new
user = sdp1.create_user
user.profile_type
> 'SomeProf'
即使在子类中声明关联,profile_type 仍然是 SomeProf。
为什么会这样?有什么方法 profile_type 匹配子类而不是父类?
【问题讨论】:
标签: ruby-on-rails inheritance polymorphic-associations