我有兩個表:多belongs_to的同一個表
貨幣和利率
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 2013-02-24 20:14:35
@JaredMcAteer我有top_currency_id列,我認爲Rails應該查找top_currency的top_currency_id列。 – Grisha 2013-02-24 20:17:24