Active Record 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
但这给了我
`日志中的救援':PGError:错误:缺少表“游戏”的 FROM 子句条目(ActiveRecord::StatementInvalid) 第 1 行:...games" ON "games"."id" = "offers"."game_id" WHERE "game"."se... :选择“offers”。* FROM“offers”INNER JOIN“games”ON“games”。“id”=“offers”。“game_id”其中“game”。“season”= 2009 AND(“offers”.broker_id = 1)
我应该如何进行这样的选择,或者我在哪里可以找到更多这方面的信息?
i've got these classes:
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
and i want to select all offers from one broker where the season of the game is 2009, for example.
i tried
Broker.first.offers.joins(:game).where(:game => {:season => 2009}).each do |o|
puts o.inspect
end
but that gives me
`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)
how should i do such selects, or where can i find more information on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
where(:game => {:season => 2009})
更改为where(:games => {:season => 2009})
您的表格名为“games”(复数形式),where 条件中的哈希键应该是表名,而不是关联名。
Change
where(:game => {:season => 2009})
towhere(:games => {:season => 2009})
Your table is named "games" (in plural) and the hash key in the where condition should be the table name, not the association name.