我下面就ActiveRecord的一个在线教程,其中定义了一个表和它的关系时,教练写了下面的代码:ActiveRecord:模型中的数据库关系定义与表设置中的外键定义?
#Setup of the database table
class CreateTimeEntries < ActiveRecord::Migration
def change
create_table :time_entries do |t|
t.float :time
t.belongs_to :customer
t.belongs_to :employee
t.timestamps
end
end
end
#Relationship definition in the relevant model
class TimeEntry < ActiveRecord::Base
belongs_to :customer
belongs_to :employee
end
不是那些线路冗余?
#in table setup
t.belongs_to :customer
t.belongs_to :employee
#in the relevant model
belongs_to :customer
belongs_to :employee
我了解,在db表设置的线路都在这里定义外键,怎么就那么我们需要在模型中定义的关系呢? 我认为外键自己定义了这种关系。
我在这里错过了什么?在网上找不到明确的答案。非常感谢。
晶莹剔透。非常感谢 ! – 2014-11-03 10:25:37