开发者

Inheritance and polymorphic-associations in rails

I have a User model, which belongs to Profile (belongs_to polymorphic). One model comes in two subclasses, but the profile_type in User always correspond to the parent model.

User < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true

SomeProf < ActiveRecord::Base
  has_one :user, :as => :profile

SomeDeepProf1 < SomeProf

SomeDeepProf2 < SomeProf

Then:

sdp1 = SomeDeepProf1.new
user = sdp1.create_user
user.profile_type
> 'SomeProf'

Even stating the association in sub开发者_运维问答classes, the profile_type remains SomeProf.

Why does this happen? Is there any way profile_type match subclass and not the parent class?


This happens because the _type column is supposed to identify the model's table and should not contain data on the model itself - just a reference.

However if you inspect user.profile.type it should return SomeDeepProf1.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜