【发布时间】:2013-02-24 20:11:59
【问题描述】:
我有两张桌子:
货币和汇率
currencies: id:int, code:string, name: string
rates: id:int, top_currency_id:int, bottom_currency_id:int, rate:float
我有两个活动记录:
class Rate < ActiveRecord::Base
attr_accessible :bottom_currency, :rate, :top_currency, :top_currency_id
belongs_to :top_currency, :class_name => 'Currency', :foreign_key => 'top_currency_id'
belongs_to :bottom_currency, :class_name => 'Currency', :foreign_key => 'bottom_currency_id'
end
class Currency < ActiveRecord::Base
attr_accessible :code, :name
has_many :rates
end
所以问题是: 当我试图执行以下代码时: top_currency = Currency.find_by_id(1) @test = Rate.where(:top_currency=>top_currency)
我收到以下错误:
Mysql2::Error: Unknown column 'rates.top_currency' in
'where clause': SELECT `rates`.* FROM `rates` WHERE `rates`.`top_currency` = 1
为什么 Rails 的魔法不起作用?
非常感谢。
【问题讨论】:
-
错误指出
top_currency不是rates表中的列,您确定将更改迁移到您正在使用的环境中吗? -
@JaredMcAteer 我有 top_currency_id 列,我认为 Rails 应该寻找 top_currency_id 列 insted of top_currency。
标签: ruby-on-rails ruby-on-rails-3 model-associations