2009-10-05 21 views
1

如果我有問題和解答之間的has_manybelongs_to關係:我是否需要在「問題」和「答案」模型中都有「引用」列?

class Question < ActiveRecord::Base 
    has_many :answers 
end 

class Answer < ActiveRecord::Base 
    belongs_to :question 
end 

我是否還需要修改遷移文件使用「引用」:

class CreateAnswers < ActiveRecord::Migration 
    def self.up 
    create_table :answers do |t| 
     t.text :body 
     t.references :question 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :answers 
    end 
end 

class CreateQuestions < ActiveRecord::Migration 
    def self.up 
    create_table :questions do |t| 
     t.text :body 
     t.references :answer 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :questions 
    end 
end 

回答

2

外鍵唯一無二的其他型號爲belongs_to

相關問題