0
一個'user
有許多posts
和product
有許多posts
,和任何給定的post
可以屬於一個user
或product
但不能同時。在Rails中用一個模型存儲兩個has_many關係的最簡單方法是什麼?
我認爲存儲在posts_relationships
表has_many :through
關係,這樣寫:
Class User < ActiveRecord::Base
has_many :posts, :through => posts_relationships
和
Class Product < ActiveRecord::Base
has_many :posts, :through => posts_relationships
會表達什麼,我需要。 這是正確和最簡單的方法嗎?這不是一個複雜的關係,所以我想盡可能簡單地編寫它。
這聽起來像我需要什麼。我只是看着在回答[這](http://stackoverflow.com/questions/697840/polymorphic-associations-in-rails)問題的截屏,但它很老了。我將考慮有關多態關聯的Rails 3特定教程。 – pthesis 2012-03-17 01:40:30
的截屏是沒有太大的不同(如果有的話)從實現的,它只是可能很難環繞在對象範圍內思考這些問題。真的,這只是一個外鍵關係,一旦你開始思考這種方式,你就會開始意識到實現沒有那麼不同。以下是[多態關聯文檔(http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations) – Azolo 2012-03-17 05:08:29
吡唑並感謝的鏈接! – pthesis 2012-03-18 11:50:13