我下面就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