2013-03-11 196 views
3

我有以下型號:recommendations,ratingsproduct。一個producthas_manyrecommendations,一個recommendationhas_manyproducts,一個recommendationhas_manyratingsratingbelongs_to一個recommendation
我目前使用以下查詢找到我的索引的所有記錄:Rails 3與has_many協會查詢

@recommendations = Recommendation.find(:all, :joins => :products, :conditions => ["product_id = ? AND rating_set = ?", (params["product_id"]), (params["rating_set_id"])]) 

,並得到各記錄推薦評級,我還有一個範圍與我在查看通話。

我該怎麼設置@recommendatons以找回特定product_idrating_set找到的所有建議和評分?

我想:

Recommendation.find(:all, :joins => :products,:include => :ratings, :conditions => ["product_id = ? AND rating_set = ?",2086981,40]).each do |rec| 
puts rec.rating 
end 

這些印刷Nil

配車型更新:

產品

class Product < ActiveRecord::Base 
    has_many :product_recommendations, :dependent => :destroy 
    has_many :recommendations, :through => :product_recommendations 
end 

建議

class Recommendation < ActiveRecord::Base 
    has_many :product_recommendations, :dependent => :destroy 
    has_many :products, :through => :product_recommendations 
    has_many :ratings 
end 

評級:

class Rating < ActiveRecord::Base 
    belongs_to :recommendation 
end 

這是我需要的結果(如果等級已經創建這隻作品,這就是爲什麼我不能使用它):

@recommendations = Rating.find(:all, :conditions => ["product_id = ? AND rating_set = ?", (params["product_id"]), (params["rating_set_id"])]) 

什麼即時查詢:我需要找到所有建議 其中的product_id和rating_set =? ,?並且鑑於此,找到屬於這些建議中的每一個的所有評級。

更新

我能夠回到使用下面的查詢屬於推薦的所有評級: Recommendation.joins(:product_recommendations).includes(:ratings).where(:product_recommendations => { :rating_set => 48, :product_id => 2144877 }) 當我遍歷收視陣列的時候,他們都沒有作用域正確rating_set ,而是我將所有rating_sets的所有評級都歸於特定推薦。如何獲得每個推薦和rating_set的評分數組。

+0

是什麼'Recommendation.find(:全部:聯接=>:產品:包括=>:評級)'返回? – 2013-03-11 23:52:43

+0

它返回以下查詢:'SELECT recommendations。* FROM recommendations INNER JOIN product_recommendations ON product_recommendations.recommendation_id = recommendations.id INNER JOIN products ON products.id = product_recommendations.product_id'。 – Yogzzz 2013-03-12 02:25:18

+0

我的意思是多少行? – 2013-03-12 02:47:31

回答

0

試試這個,

recommendations = Recommendation.joins(:product).includes(:ratings).where(:product_id => 2086981, :rating_set => 40).all 
recommendations.each do |rec| 
    puts rec.ratings # this will return an array of ratings if this recommendation has ratings. 
end 

我覺得你:joins => :products應該是單數::joins => :product和你:include應該是:includes

編輯:

的關係,在新的模式密碼相匹配的新查詢發佈:

recommendations = Recommendation.joins(:products, :product_recommendations).includes(:ratings).where(:products => { :id => 2086981 }, : product_recommendations => { :rating_set => 40 }).all 
recommendations.each do |rec| 
    puts rec.ratings # this will return an array of ratings if this recommendation has ratings. 
end 
+0

這不起作用,它需要是「產品」。如果我使用產品,我會得到一個沒有發現異常的關聯。如果我將其更改爲'產品'與上述相同的查詢,我會得到:'Mysql2 ::錯誤:未知列'recommendations.product_id'在'where子句'中:SELECT建議。* FROM建議INNER JOIN product_recommendations ON product_recommendations.recommendation_id = recommendations.id INNER JOIN products on products.id = product_recommendations.product_id WHERE recommendations.product_id = 2086981 AND recommendations.rating_set = 40' – Yogzzz 2013-03-12 02:29:35

+0

您的問題描述與您的型號代碼不符。 「推薦''belongs_to'產品'」,但您的模型代碼有'has_many:products'。另外,因爲您的'推薦'模型不'屬於'產品',所以您不能具有以下條件:'.where(:product_id => 2086981,...'因爲您的'推薦'模型沒有一個'product_id'列我用新的查詢更新了我的答案 – Sam 2013-03-12 07:34:56

+0

對不起,更新的問題我添加的模型是正確的 – Yogzzz 2013-03-12 08:44:31

0

實際上是爲你描述它的數據模型?你的初始描述似乎表明它是所有父母 - >孩子(一對多)關係。產品具有一個一對多與建議,並建議有一個一對多的評級

這將意味着一個表的結構是這樣的:

products table 
:id 

recommendations table 
:id 
:product_id 

ratings table 
:id 
:recommendation_id 

但是,所有生成的SQL查詢您發佈似乎以顯示某種名爲product_recommendations的連接表。那是什麼? 實際上,產品和推薦之間有多對多的關係,其中有一個名爲product_recommendations的連接表?

如果是這樣,我會說你的查詢應該可以工作,但你需要通過定義字段不是has_manybelongs_to而是has_and_belongs_to_many產品和推薦模型。

看看這有助於http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

+0

更新我的模型信息。 – Yogzzz 2013-03-12 04:41:35