rails中的inheritance和多态关联
我有一个User模型,属于Profile (belongs_to polymorphic)。 一个模型有两个子类,但User中的profile_type始终对应于父模型。
User true SomeProf :profile SomeDeepProf1 < SomeProf SomeDeepProf2 < SomeProf
然后:
sdp1 = SomeDeepProf1.new user = sdp1.create_user user.profile_type > 'SomeProf'
即使在子类中声明关联, profile_type仍然是SomeProf 。
为什么会这样? 有没有什么方法profile_type匹配子类而不是父类?
发生这种情况是因为_type
列应该标识模型的表,并且不应该包含模型本身的数据 – 只是一个引用。
但是,如果您检查user.profile.type
它应该返回SomeDeepProf1
。