不同的声明方式带来的区别是外键放在哪个模型对应的数据表中(外键在声明 belongs_to 关联所在模型对应的数据表中)。
class Supplier < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
end
==============
class CreateSuppliers < ActiveRecord::Migration
def change
create_table :suppliers do |t|
t.string :name
t.timestamps
end
create_table :accounts do |t|
t.integer :supplier_id #外键在声明 belongs_to 关联所在模型对应的数据表中
t.string :account_number
t.timestamps
end
end
end