Rails:创建Model同时加入table,has_many通过

Rails: Create Model and join table at the same time, has_many through

我有三个模型:

class Question < ActiveRecord::Base
  has_many :factor_questions
  has_many :bigfivefactors, through: :factor_questions

  accepts_nested_attributes_for :factor_questions
  accepts_nested_attributes_for :bigfivefactors
end

class Bigfivefactor < ActiveRecord::Base
  has_many :factor_questions
  has_many :questions, through: :factor_questions
end

和我的 join-table,它不仅包含 bigfivefactor_idquestion_id,还有另一个整数列 value.

class FactorQuestion < ActiveRecord::Base
  belongs_to :bigfivefactor
  belongs_to :question
end

创建一个新的 Question 工作正常,在我的 _form.html.erb

中使用
<%= form_for(@question) do |f| %>
  <div class="field">
    <%= f.label :questiontext %><br>
    <%= f.text_field :questiontext %>
  </div>

<%= f.collection_check_boxes :bigfivefactor_ids, Bigfivefactor.all, :id, :name do |cb| %>
  <p><%= cb.check_box + cb.text %></p>
<% end %>

我可以根据需要勾选或取消勾选 bigfivefactors

但是,正如我之前提到的,连接模型还包含一个 value

问题:

如何在 add/edit 和 'value' 的每个复选框旁边即时添加文本字段?

For better understanding, i added an image

在控制台中,我基本上可以这样做:

q= Question.create(questiontext: "A new Question")
b5 = Bigfivefactor.create(name: "Neuroticism")
q.bigfivefactors << FactorQuestion.create(question: q, bigfivefactor: b5, value: 10)

我还发现要编辑我的 questions_controller:

def new
  @question = Question.new
  @question.factor_questions.build
end

但我不知道如何将其放入我的视图中。

非常感谢您的帮助!

大五因素模型注意事项

您的 Bigfivefactors 似乎不应该随着问题的每次更新而修改。我实际上假设这些将是 CMS 控制的字段(以便管理员定义它们)。如果是这种情况,请删除问题模型中 bigfivefactors 的 accepts_nested_attributes。这将允许参数注入,这将改变整个站点的行为。您希望能够 link 到现有的 bigfivefactors,因此 @question.factor_questions.first.bigfivefactor.name 是标签,@question.factor_questions.first.value 是值。请注意,这些存在于对象模型的不同 'planes' 上,因此我们在这里无能为力。

参数

为了传递您正在寻找的嵌套属性,参数需要如下所示:

params = { 
 question: { 
   questiontext: "What is the average air speed velocity of a sparrow?", 
   factor_questions_attributes: [ 
     { bigfivefactor_id: 1, value: 10 }, 
     { bigfivefactor_id: 2, value: 5  } ]
    } 
  }

一旦我们有了类似的参数,运行 Question.create(params[:question]) 将创建问题和相关的 @question.factor_questions。为了创建这样的参数,我们需要 html 表单复选框元素,名称为 "question[factor_questions_attributes][0][bigfivefactor_id]",值为“1”,然后是名称为 "question[factor_question_attributes][0][value]"[=21 的文本框=]

Api: nested_attributes_for has_many

查看

这是您需要使用 fields_for 通过帮助程序字段构建嵌套属性所需的视图。

<%= f.fields_for :factor_questions do |factors| %>
  <%= factors.collection_check_boxes( :bigfivefactor_id, Bigfivefactor.all, :id, :name) do |cb| %>
    <p><%= cb.check_box + cb.text %><%= factors.text_field :value %></p>
  <% end %>
<% end %>

API: fields_for

我不确定它们是如何在视图中组合在一起的。您可能无法使用内置的帮助程序。您可能需要创建自己的收集助手。 @question.factor_questions。喜欢:

<%= f.fields_for :factor_questions do |factors| %>
  <%= factors.check_box :_destroy, {checked => factors.object.persisted?}, '0','1' %> # display all existing checked boxes in form
  <%= factors.label :_destroy, factors.object.bigfivefactor.name %>
  <%= factors.text_box :value %>
  <%= (Bigfivefactor.all - @question.bigfivefactors).each do |bff| %>
    <%= factors.check_box bff.id + bff.name %><%= factors.text_field :value %></p> # add check boxes that aren't currently checked
  <% end %>
<% end %>

老实说,我知道这不能正常使用。我希望对参数的了解有所帮助,但如果无法访问实际的 rails 控制台,我怀疑我能否创建代码来完成您正在寻找的内容。这是一个有用的 link:Site point does Complex nested queries