Skrypter Skrypter - 6 months ago 20
MySQL Question

update join table has_many through with additional checkboxes

if i dont find solution here i have no idea where i should looking for...

I know there is right, easy solution for it...but something i just dont understand.

I have 3 models...

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 have 3 additional fields...with type: boolean.

enter image description here




I'd like to update scheme_custom_issue_field_definitions table. How controller and form should looks like?



Additional image:
enter image description here

Update:

In console i can update it like this:

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)


Next Update:

Now form looks like this (what is completely wrong):

<%= 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 %>


and 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


Update Solution:
enter image description here

Answer

If you want to create new SchemCustomIssueFieldDefinition record in the console, I suggest this alternative:

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

This will save you one less query than your current example code as this will not do have an UPDATE sql.

For your form:

<%= 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
Comments