use*_*335 1 ruby activerecord nested ruby-on-rails-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
Run Code Online (Sandbox Code Playgroud)
例如,我想从一个经纪人选择2009年游戏季节的所有优惠.我试过了
Broker.first.offers.joins(:game).where(:game => {:season => 2009}).each do |o|
puts o.inspect
end
Run Code Online (Sandbox Code Playgroud)
但这给了我
`救援日志":PGError:错误:缺少FROM子句为表 "游戏" 项(ActiveRecord的:: StatementInvalid)线路1:......游戏" ON "游戏ID "= "报价game_id "".""." WHERE "游戏". "SE ...:选择 "提供大量的 "INNER JOIN "游戏" ON "游戏ID" = "报价game_id "WHERE "游戏的季节 ""*FROM.""."".""." = 2009 AND("offer".broker_id = 1)
我该怎么做这样的选择,或者我在哪里可以找到更多相关信息?
Teo*_*las 13
更改where(:game => {:season => 2009})到where(:games => {:season => 2009})
您的表被命名为"游戏"(复数形式),where条件中的散列键应该是表名,而不是关联名.
| 归档时间: |
|
| 查看次数: |
1762 次 |
| 最近记录: |