1
如果我有問題和解答之間的has_many
和belongs_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