更新连接表has_many以及其他复选框

时间:2016-05-30 12:39:43

标签: mysql ruby-on-rails ruby-on-rails-4 simple-form

如果我在这里找不到解决方案,我不知道我应该在哪里寻找......

我知道它有正确,简单的解决方案......但我不明白的事情。

我有3个模特......

Scheme.rb

class Scheme < ActiveRecord::Base
  has_many :projects
  has_many :custom_issue_field_definitions, through: :scheme_custom_issue_field_definitions
  has_many :scheme_custom_issue_field_definitions
end

CustomIssueFieldDefinition.rb

class CustomIssueFieldDefinition < ActiveRecord::Base
  has_many :schemes, through: :scheme_custom_issue_field_definitions
  has_many :scheme_custom_issue_field_definitions
  belongs_to :custom_issue_field
end

SchemeCustomIssueFieldDefinition.rb

class SchemeCustomIssueFieldDefinition < ActiveRecord::Base
  belongs_to :scheme
  belongs_to :custom_issue_field_definition
end

Join Model有3个额外的字段......类型:boolean。

enter image description here

我想更新scheme_custom_issue_field_definitions表。控制器和表单应该如何?

其他图片: enter image description here

更新:

在控制台中,我可以像这样更新它:

a = Scheme.first
b = CustomIssueFieldDefinition.first
c = a.scheme_custom_issue_field_defintitions.find_or_create_by(custom_issue_field_definition: b)
c.update_attributes(visible: 1, reportable: 0, required: 0)

下一次更新:

现在表单看起来像这样(完全错误):

<%= simple_form_for @scheme_new_custom_issue_field, url: configurations_scheme_path(@scheme), method: :put do |f| %>
      <% @available_custom_issue_field_definitions.each do |custom_issue_field| %>
        <tr>
            <td><%= custom_issue_field.label %></td>
            <td><%= f.input :visible, as: :boolean %></td>
            <td><%= f.input :reportable, as: :boolean %></td>
            <td><%= f.input :required, as: :boolean %></td>
        </tr>
      <% end %>
      <%= f.button :submit, class: "btn btn-primary" %>
    <% end %> 

和schemes_controller

def update
    @scheme = Scheme.find(params[:id])
    @scheme_new_custom_issue_field = @scheme.scheme_custom_issue_field_definitions.
      find_or_create_by(scheme_id: @scheme, custom_issue_field_definition_id: params[:custom_issue_field_definition_id])
    if @scheme_new_custom_issue_field.update_attributes(scheme_custom_issue_field_definition_params)
      flash[:success] = "Scheme has been successfully updated"
      redirect_to :back
    else
      render :edit
    end
  end

更新解决方案: enter image description here

1 个答案:

答案 0 :(得分:1)

如果您想在控制台中创建新的SchemCustomIssueFieldDefinition记录,我建议使用此备选方案:

a = Scheme.first
b = CustomIssueFieldDefinition.first
c = SchemeCustomIssueFieldDefinition.find_or_initialize_by(scheme: a, custom_issue_field_definition: b)
c.assign_attributes(visible: 1, reportable: 0, required: 0)
c.save

这将比您当前的示例代码节省少一个查询,因为这不会有UPDATE sql。

表格:

<%= simple_form_for @scheme, method: :patch do |f| %>
  <% @available_custom_issue_field_definitions.each do |custom_issue_field| %>
    <tr> 
      <%= f.simple_fields_for :scheme_custom_issue_field_definitions, @scheme.scheme_custom_issue_field_definitions.find_or_initialize_by(custom_issue_field_definition: custom_issue_field) do |ff| %>
        <%= ff.hidden_field :id %>
        <%= ff.hidden_field :scheme_id, value: @scheme.id %>
        <%= ff.hidden_field :custom_issue_field_definition_id, value: custom_issue_field.id %>
        <td><%= custom_issue_field.label %></td>
        <td><%= ff.input :visible, as: :boolean %></td>
        <td><%= ff.input :reportable, as: :boolean %></td>
        <td><%= ff.input :required, as: :boolean %></td>
      <% end %>
    </tr>
  <% end %>
  <%= f.button :submit, class: "btn btn-primary" %>
<% end %>

schemes_controller.rb:

class SchemesController < ApplicationController
  # ...
  def update
    @scheme = Scheme.find(params[:id])
    if @scheme.update(scheme_params)
      flash[:success] = "Scheme has been successfully updated"
      redirect_to :back
    else
     render :edit
    end
  end

  # ...

  private

  def scheme_params
    # update below if you need other scheme attributes to be updated
    params.require(:scheme).permit(:id, scheme_custom_issue_field_definitions_attributes: [:id, :scheme_id, :custom_issue_field_definition_id, :visible, :reportable, :required])
  end
end

scheme.rb

class Scheme < ActiveRecord::Base
  # ...
  accepts_nested_attributes_for :scheme_custom_issue_field_definitions_attributes
  # ...
end