Active Record 3 使用嵌套表进行选择

发布于 2024-10-27 04:12:02 字数 840 浏览 1 评论 0原文

我有这些课程:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-11-03 04:12:02

where(:game => {:season => 2009}) 更改为 where(:games => {:season => 2009})

您的表格名为“games”(复数形式),where 条件中的哈希键应该是表名,而不是关联名。

Change where(:game => {:season => 2009}) to where(: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文