【发布时间】:2013-02-09 21:01:39
【问题描述】:
我正在尝试为我的用户创建不同的个人资料类型。
我有一个用户模型。
User 类型有一个Profile 相关,所以它@987654323@ 但是,Page 类型有一个Page 相关,所以它@987654326@
但是,我对两者都使用相同的 User 表,并且我正在设置帐户类型。
我想知道,如何根据我的用户帐户类型确定这种关系
编辑
用户模型 has_one :profile Profile belongs_to :user Page belongs_to :user 帐户类型是“User”(进入 Profile 模型)或“Page”(进入 Page 模型)。
class User < ActiveRecord::Base
has_one :profile, :class_name => 'Here it should be either PROFILE or PAGE'
end
class Profile < ActiveRecord::Base
belongs_to :user
end
class Page < ActiveRecord::Base
belongs_to :user
end
我已经通过 API 阅读了一下,发现 :class_name,现在我的挑战是动态确定它。
编辑
稍微修改了页面模型和用户模型。
【问题讨论】:
-
请发布您的模型及其关联。 “帐户类型”是指“个人资料”吗?
-
用户模型 has_one :profile Profile belongs_to :user Page belongs_to :user 账户类型要么是“User”(进入 Profile 模型),要么是“Page”(进入 Page 模型)我已经通过API阅读了一下,发现:class_name,现在我的挑战是动态确定它。
-
如果我理解正确,
User有一个属性account_type,它是Page或User.profile的一个实例? -
没错!但是,在 Page 模型中,没有 has_one :page。你可以看到我对我的主要帖子所做的更改。提前感谢您的帮助!
-
那么是什么决定了
User的帐户类型是Page还是User.profile?设置User.account_type的条件是什么?
标签: ruby-on-rails has-one