型號:
class User < ActiveRecord::Base
has_many :teams
end
class League < ActiveRecord::Base
belongs_to :user
has_many :teams
end
class Team < ActiveRecord::Base
belongs_to :league
belongs_to :user
end
遷移:
create_table(:users) do |t|
t.string :name
t.timestamps
end
create_table(:leagues) do |t|
t.references :user, index: true
t.string :name
t.timestamps
end
create_table(:teams) do |t|
t.references :league, index: true
t.references :user, index: true
t.string :name
t.timestamps
end
我建議在Rails的指南,瞭解associations和migrations。
感謝您的幫助!顯然,我在做一個無關的錯誤來運行我的測試,導致我相信我的模型彼此之間存在着某種問題。 –