我有以下型號,與他們相關協會:Rails的 - 需要幫助建立鬆散耦合協會
class User < ActiveRecord::Base
has_many :reviews
has_many :ratings
end
class Product < ActiveRecord::Base
has_many :reviews
has_many :ratings
end
class Review < ActiveRecord::Base
belongs_to :product
belongs_to :user
end
class Rating < ActiveRecord::Base
belongs_to :product
belongs_to :user
end
給出了具體的Rating
,我需要得到相應的Review
(如果您的評價存在)。
我需要保持ratings
和reviews
鬆耦合。 (我不想設置我的模式,使一個Review
belongs_to
一個Rating
)
我應該怎樣建立一個rating's
協會reviews
?
一旦我在視圖中使用特定的評分,我可以撥打@rating.product.reviews.where(:user_id => @rating.user.id).first
,但如果可能,我希望它更清潔/更高效。
任何想法?
謝謝。
他並不需要指定用戶兩次 - 「user.reviews.where(:product => product)」就足夠了。 –
是的,謝謝,留下了一個意外,固定。 – jimworm
你能解釋一下這裏發生了什麼:「{:product_id =>&:product_id}」「?謝謝。 – johnnycakes