【发布时间】:2013-07-06 18:18:21
【问题描述】:
我有
User那个has_one :profile。我有
Profile,它有一些属性,每个个人资料都有共同点。我希望一些
Users具有Coach_profile或Student_profile的Sub_profile。
我打算在Profile 和这些Sub_profiles 之间使用多态关系,以允许每个User 拥有它们的基本Profile,然后拥有它们相应的Sub_profile。
这就是我纠结的地方。
我很难确定 belongs_to、has_one :profile、as: :sub_profile polymorphic: true 和 dependent: :destroy 方法都属于哪里。
此时,了解如何构建此类解决方案的人将能够写出这些关系 - 但我觉得我需要解释我的(有缺陷的)推理,以便以我的方式构建它,以便有人可以帮助我明白为什么我做错了。
我的问题:
同样重要的是要注意,以防不明显,以下代码结构不会产生实际工作所需的代码(如上所述)。我遇到如下错误:
> Coach_profile.create
RuntimeError: Circular dependency detected while autoloading constant Coach_profile
并尝试类似:
> user.profile.build_coach_profile
结果为@987654340@
我的代码背后的原因:
按照我现在构建代码的方式,我无法弄清楚如何使用 build_sub_profiles 或 build_coach_profile(例如),因为我设计关系的方式不允许这样做。
- 我想要我的
Profiles到belongs_to :sub_profile, polymorphic: true, dependent: :destroy,因为我希望在我的profile表中有一个sub_profile_id,这样我就可以- 参考
profile.sub_profile - 有不同类型的
sub_profiles - 当
profile被销毁时,销毁关联的sub_profiles
- 参考
- 我想要我的
sub_profiles(coach和student个人资料)到has_one :profile, as: :sub_profile- 所以我的每个
sub_profile类型都可以通过我的profile表的sub_profile_id和sub_profile_type字段属于profile。
- 所以我的每个
用户.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile
end
profile.rb
# id :integer not null, primary key
# user_id :integer
# first_name :string(255) not null
# middle_name :string(255)
# last_name :string(255) not null
# phone_number :integer
# birth_date :date not null
# created_at :datetime
# updated_at :datetime
# sub_profile_id :integer
# sub_profile_type :string(255)
#
class Profile < ActiveRecord::Base
belongs_to :user
belongs_to :sub_profile, polymorphic: true, dependent: :destroy
validates_presence_of :first_name, :last_name, :birth_date
validates_length_of :phone_number, {is: 10 || 0}
end
coach_profile.rb
# id :integer not null, primary key
# coaching_since :date
# type_of_coach :string(255)
# bio :text
# created_at :datetime
# updated_at :datetime
#
class CoachProfile < ActiveRecord::Base
has_one :profile, as: :sub_profile
end
student_profile.rb
# id :integer not null, primary key
# playing_since :date
# competition_level :string(255)
# learn_best_by :string(255)
# desirable_coach_traits :text
# goals :text
# bio :text
# created_at :datetime
# updated_at :datetime
#
class StudentProfile < ActiveRecord::Base
has_one :profile, as: :sub_profile
end
我在这里错误地处理了一些事情。如何正确设置多个sub_profiles 和父profile 之间的多态关系?
【问题讨论】:
-
只需将 Profile 和 SubProfile 之间的 belongs_to has_one 反向,您将在几分钟内弄清楚其他所有内容。
-
我将子配置文件更改为
belongs_to :profile, polymorphic: true而不是has_one :profile, as: :sub_profile并将as: :sub_profile部分移至配置文件中的 has_one 行,因为我似乎无法使用as: :modelon一个belongs_to电话。然后,我将belongs_to :profile, as: :sub_profile添加到了 sub_profiles 的迁移中。尝试Coach_profile.create时仍然出现循环依赖错误,我想我仍然缺少一些东西。如果您有任何意见,那就太好了,但我会继续挖掘。
标签: ruby-on-rails activerecord polymorphic-associations ruby-on-rails-4