2011-03-27 78 views
1

我已經得到了這些類中進行選擇:活動記錄3嵌套表

class Game < ActiveRecord::Base 
    has_many :offers 
    #table has an integer-column 'season' 
end 

class Broker < ActiveRecord::Base 
    has_many :offers 
end 

class Offer < ActiveRecord::Base 
    belongs_to :game 
    belongs_to :broker 
end 

,我想選擇從一個經紀人那裏比賽的賽季是2009年,例如,所有的優惠。 我試圖

Broker.first.offers.joins(:game).where(:game => {:season => 2009}).each do |o| 
    puts o.inspect 
end 

但是這給了我

`rescue in log': PGError: ERROR: missing FROM-clause entry for table "game" (ActiveRecord::StatementInvalid) LINE 1: ...games" ON "games"."id" = "offers"."game_id" WHERE "game"."se... : SELECT "offers".* FROM "offers" INNER JOIN "games" ON "games"."id" = "offers"."game_id" WHERE "game"."season" = 2009 AND ("offers".broker_id = 1)

我應該怎麼做這樣的選擇,或在哪裏可以找到更多這方面的信息?

回答

13

變化where(:game => {:season => 2009})where(:games => {:season => 2009})

你的表被命名爲「遊戲」(複數)和散列鍵WHERE條件應該是表的名稱,而不是聯想的名字。

+0

謝謝,這是非常有益的! – user573335 2011-03-27 18:59:47

+0

我愛你,我一直在尋找這個小問題好幾個小時。謝謝。 – jphenow 2012-01-22 22:07:17