我有接下来的两个模型
class Currency < ActiveRecord::Base
# something ...
end
class CurrencyRate < ActiveRecord::Base
attr_accessible :rate
belongs_to :currency_to_convert, :class_name => 'Currency'
belongs_to :currency_converted, :class_name => 'Currency'
end
我应该如何为CurrencyRate模型创建迁移,只有一个字段用于外键关联或两个字段?
我一直在考虑像
这样的事情class CreateCurrencyRates < ActiveRecord::Migration
def change
create_table :currency_rates do |t|
t.integer :currency_id
t.timestamps
end
end
end
但我不确定是否适用于具有两个belongs_to关联的模型。
答案 0 :(得分:3)
class CurrencyRate < ActiveRecord::Base
attr_accessible :rate
belongs_to :currency_to_convert, :class_name => 'Currency', :foreign_key => 'currency_convert_id'
belongs_to :currency_converted, :class_name => 'Currency', :foreign_key => 'currency_converted_id'
end
class CreateCurrencyRates < ActiveRecord::Migration
def change
create_table :currency_rates do |t|
t.integer :currency_converted_id
t.integer :currency_convert_id
t.timestamps
end
end
end