3
我有以下型號:Rails的多個聯接AR查詢和where子句
class Product < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :recommendation_sets, :through => :product_recommendation_sets
end
class ProductRecommendationSet < ActiveRecord::Base
belongs_to :product
belongs_to :recommendation_set
end
class RecommendationSet < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :products, :through => :product_recommendation_sets
has_many :recommendation_recommendation_sets, :dependent => :destroy
has_many :recommendations, :through => :recommendation_recommendation_sets
end
class RecommendationRecommendationSet < ActiveRecord::Base
belongs_to :recommendation
belongs_to :recommendation_set
end
class Recommendation < ActiveRecord::Base
has_many :recommendation_recommendation_sets, :dependent => :destroy
has_many :recommendations, :through => :recommendation_recommendation_sets
end
我試着去選擇所有recommendations
其中的product_id = x
,這樣做:
RecommendationSet.joins(:products, :recommendations).where(product_id:1)
但是我得到一個未知列錯誤。我如何加入按給定的product_id選擇所有建議。 Psudo code: 查找recommendation_sets
其中product_id = ?
。查找recommendations
,其中recommendation_set_id = ?
。
謝謝!這將返回所有'RecommendationSets',我怎樣才能返回所有的'建議',所以像'RecommendationSet.joins(:products,:recommendations).where(products:{id:1})。recommendations' – Yogzzz
是的你可以在返回列表的每個元素上調用.recommendations;) – MrYoshiji