如何在 rails 中没有直接关联的 2 个模型之间进行关联?
How to associate between 2 models which are not directly related in rails?
我有 5 个tables/models。这是结构。
1. application
id
name
2. profiles
id
name
app_id (Foreign key application table: id)
3. app_profiles
id
profile_id (Foreign key profile table: id)
app_id (Foreign key application table: id)
4. segments
id
app_id (Foreign key application table: id)
5. profile_segments
id
app_profile_id (Foreign key app_profile table: id)
segment_id (Foreign key segment table: id)
这是我的联想:
application.rb
has_many :profiles, through: :app_profiles
has_many :segments
has_many :app_profiles
profile.rb
has_many :applications, through: :app_profiles
has_many :app_profiles
app_profile.rb
belongs_to :profile
belongs_to :application
has_many :segments, through: :profile_segments
has_many :profile_segments
segment.rb
has_many :profiles, through: :app_profiles
has_many :app_profiles, through: :profile_segments
has_many :profile_segments
belongs_to :application
profile_segment.rb
belongs_to :app_profile
belongs_to :segment
现在的问题是我需要在配置文件和 profile_segment 之间建立关联。这可以做到吗?如果有怎么办?
在此先感谢您的帮助!
在profile.rb中添加:
has_many :profile_segments, through: :app_profiles
Rails 支持链式 has_many through:
语句。您的 AppProfile
模型已经 has_many :profile_segments
和 Profile
has_many :app_profiles
,因此您可以链接它们。
在后台,当您执行 profile.profile_segments
之类的操作时,ActiveRecord
将执行 2 INNER JOIN
秒。您可以在 rails server
的调试输出中看到完整的 SQL 语句。甚至我都不确定 rails 究竟是如何发挥它的魔力的:D
我可以说,我不知道你想要实现什么,但你的结构非常复杂。尝试删除一些模型 - 可能会将它们添加为其他模型中的某些列。
我必须这样做才能理解你的结构:
profile_segment
通过 2 条路径属于 application
。绝对不是什么好事。也许它应该属于 profile
而不是 app_profile
我有 5 个tables/models。这是结构。
1. application
id
name
2. profiles
id
name
app_id (Foreign key application table: id)
3. app_profiles
id
profile_id (Foreign key profile table: id)
app_id (Foreign key application table: id)
4. segments
id
app_id (Foreign key application table: id)
5. profile_segments
id
app_profile_id (Foreign key app_profile table: id)
segment_id (Foreign key segment table: id)
这是我的联想:
application.rb
has_many :profiles, through: :app_profiles
has_many :segments
has_many :app_profiles
profile.rb
has_many :applications, through: :app_profiles
has_many :app_profiles
app_profile.rb
belongs_to :profile
belongs_to :application
has_many :segments, through: :profile_segments
has_many :profile_segments
segment.rb
has_many :profiles, through: :app_profiles
has_many :app_profiles, through: :profile_segments
has_many :profile_segments
belongs_to :application
profile_segment.rb
belongs_to :app_profile
belongs_to :segment
现在的问题是我需要在配置文件和 profile_segment 之间建立关联。这可以做到吗?如果有怎么办?
在此先感谢您的帮助!
在profile.rb中添加:
has_many :profile_segments, through: :app_profiles
Rails 支持链式 has_many through:
语句。您的 AppProfile
模型已经 has_many :profile_segments
和 Profile
has_many :app_profiles
,因此您可以链接它们。
在后台,当您执行 profile.profile_segments
之类的操作时,ActiveRecord
将执行 2 INNER JOIN
秒。您可以在 rails server
的调试输出中看到完整的 SQL 语句。甚至我都不确定 rails 究竟是如何发挥它的魔力的:D
我可以说,我不知道你想要实现什么,但你的结构非常复杂。尝试删除一些模型 - 可能会将它们添加为其他模型中的某些列。
我必须这样做才能理解你的结构:
profile_segment
通过 2 条路径属于 application
。绝对不是什么好事。也许它应该属于 profile
而不是 app_profile