【问题标题】:Rails: Create Model and join table at the same time, has_many throughRails:同时创建Model和join table,has_many通过
【发布时间】:2015-12-26 23:26:15
【问题描述】:

我有三个模型:

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

还有我的连接表,它不仅包含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

问题:

如何在每个复选框旁边添加一个文本字段以即时添加/编辑“值”?

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

但我不知道如何将其纳入我的观点。

非常感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord associations


    【解决方案1】:

    大五因素模型注意事项

    看起来您的 Bigfivefactors 不应该在每次更新问题时进行修改。我实际上假设这些将是 CMS 控制的字段(以便管理员定义它们)。如果是这种情况,请删除问题模型中 bigfivefactors 的接受嵌套属性。这将允许参数注入,这将改变整个站点的行为。您希望能够链接到现有的 bigfivefactors,因此 @question.factor_questions.first.bigfivefactor.name 是标签,@question.factor_questions.first.value 是值。请注意,这些存在于对象模型的不同“平面”上,所以我们在这里不会有太多的魔法。

    参数

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

    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。为了创建这样的参数,我们需要名称为“question[factor_questions_attributes][0][bigfivefactor_id]”且值为“1”的 html 表单复选框元素,然后是名称为“question[factor_question_attributes] 的文本框[0][值]"

    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 控制台,我怀疑我可以创建完成您正在寻找的代码的代码。这是一个有用的链接:Site point does Complex nested queries

    【讨论】:

    • 新年快乐!虽然您的帖子开箱即用,但我可以弄清楚如何做到这一点,谢谢。将尽快用我的解决方案更新这个问题。
    • 我实际上正在尝试实现这个问题所要求的完全相同的事情。只是永远无法正确表达它!更重要的是,我一直想知道 Rails 这样做的“惯例”是什么?它让我把头发拉出来!一分钟我认为在控制器中建立关联记录是有意义的——在我看来调用 X.all 感觉不对。我认为不需要删除 Accepts_nested_attributes - 我现在将尝试使用它。我只是在尝试学习 Rails,而且我好像在墓地里挑选,因为每个问题都是从 00 年代初开始的!
    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2016-02-19
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多