我有三个模型,定义如下:
答题卡
class AnswerSheet < ActiveRecord::Base
has_many :answer_sections
accepts_nested_attributes for :answer_sections
end
回答部分
class AnswerSection < ActiveRecord::Base
belongs_to :answer_sheet
has_many :answers
accepts_nested_attributes_for :answers
end
答案
class Answers < ActiveRecord::Base
belongs_to: answer_section
end
AnswerSheet
我在模型中也定义了以下方法
def self.build_with_answer_sections
answer_sheet = new # new should be called on the class e.g. AnswerSheet.new
4.times do |n|
answer_sheet.answer_sections.build
end
answer_sheet
end
我将如何制作它,以便当我制作 AnswerSheet 的新实例时,我也可以生成它的所有依赖模型?