我有以下型號:Organisation
,Valuation
和Capability
。Rails協會多對多通過加入表
Organisation
模型曾經有一個country字段,但它引起了我的注意,它可能有多個位置。
爲了解決這個問題,我可以創造一個Location
模型,並添加Organisation
和Location
之間的關係has_many
,但是同樣的位置可以屬於多個組織。例如,組織X可能在英國,德國和美國有一個位置,組織Y可能在英國和德國有一個位置。
更大的問題在於Valuation
描述在特定位置一個組織的估值爲特定的功能。那麼,在組織X中的位置Y中的估值可能是10,它可以爲組織X是8時位置Z.
眼下Valuation
有belongs_to
關聯設定既Organisation
和Capability
:
class Valuation < ApplicationRecord
belongs_to :organisation
belongs_to :capability
end
要帳戶對於位置,我需要添加另一個關聯,但我正在尋找關於哪一個的提示。
接下來的問題是,如何設置我的關聯設定,所以我可以問這樣的問題:
「什麼是組織X的有能力Ÿ在所有位置上的平均估值」
或
「是什麼organsiation x的對能力Ÿ位置z處估值」
編輯
結束了與該many-to-many
方法去,結束了該車型:
class Organisation < ApplicationRecord
has_many :memberships
has_many :locations, through: :memberships
end
class Location < ApplicationRecord
has_many :memberships
has_many :organisations, through: :memberships
end
class Membership < ApplicationRecord
belongs_to :organisation
belongs_to :location
end
現在的問題是這個鏈接到Valuation
現在可以幫助整個答案,但如何使用複合主鍵進行評估? 含義,一個評估只能存在一個組織和一個位置? 而且,組織 - 位置關係應該是多對多 – lcguida
另外,Capability與其他實體的關係又是什麼? – lcguida
能力沒有關係,缺少Valution'belongs_to'能力 –