首页 > 解决方案 > Rails:如何根据 foriegn_key 自动循环创建项目

问题描述

我正在尝试学习比我一直在做的更复杂的事情。所以我开始了一个个人项目并立即卡住了。

我的模型父(Person - has_many)子(Checklist belongs_to)运行良好。

我有一个模型测试(has_many)并将其附加到问题(belongs_to),test_id 都运行良好。两种标准设置。

当在一个人(/person/1/checklists/new)上时,我想通过它找到具有相同 test_id 循环的所有问题,并将其附加到一个清单(belongs_to question)和问题(has_many checklists)。

  def create
   person = Person.find(params[:person_id])
   @checklist = person.checklists.new(ckecklist_params)
   # Above Works Fine (no idea what to do to loop through below)
   @question = Person.where(questions.test_id => person.test_id )
   @question.each do |test_builder|
      test_builder.create(?????)
   end
  end

我可以发布额外的细节和代码,只是试图超越基础。即使是指向正确方向的链接也会很棒。

标签: ruby-on-rails

解决方案


推荐阅读