title: Rails 4 Form: has_many through: checkboxes
body: <p><strong>Original question</strong></p>
  
  <p>Two resources: <code>users</code> and <code>animals.</code>  When creating a user, the client selects check boxes to say how many animals they have.  </p>
  
  <p>When the user form is submitted, it should not only create a new <code>user</code> record, but it should also create a bunch of records in the <code>animal_users</code> rich join table to represent each of the check boxes the client selected. </p>
  
  <p>The issue I think is that I am not specifying something correctly for the checkbox part within the form.  I have looked at the <a href="http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag" rel="noreferrer">checkbox_tag API</a>, the <a href="http://guides.rubyonrails.org/form_helpers.html" rel="noreferrer">Rails Guides on Forms</a>, and many websites and stackOverflow posts.    </p>
  
  <p>Thanks in advance, code below:</p>
  
  <p><strong>Original code <em>(answer code further down)</em></strong>:</p>
  
  <p><strong><em>models:</em></strong></p>
  
  <pre><code>#models/user.rb
  class User &lt; ActiveRecord::Base
    has_many :animals, through: :animal_users
    has_many :animal_users
    accepts_nested_attributes_for :animal_users, allow_destroy: true
  end
  
  #models/animal.rb
  class Animal &lt; ActiveRecord::Base
    has_many :users, through: :animal_users
    has_many :animal_users
  end
  
  #models/animal_user.rb
  class AnimalUser &lt; ActiveRecord::Base
    belongs_to :animal
    belongs_to :user
  end
  </code></pre>
  
  <p><strong>The <code>user</code> form</strong>: </p>
  
  <pre><code>#views/users/_form.html.erb
      &lt;%= form_for(@user) do |f| %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
  
    &lt;div&gt;
      &lt;% Animal.all.each do |animal| %&gt;
      &lt;label&gt;
          &lt;%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %&gt;
          &lt;%= animal.animal_name %&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Strong params within the <strong><em><code>users_controller.rb</code></em></strong></p>
  
  <pre><code>def user_params
        params.require(:user).permit(:name, animal_users_attributes: [:_destroy, :id, :user_id, :animal_id])
  end
  </code></pre>
  
  <hr>
  
  <p><strong>Answer code here</strong>:</p>
  
  <p><strong><em>models:</em></strong></p>
  
  <pre><code>#models/user.rb
  class User &lt; ActiveRecord::Base
    has_many :animals, through: :animal_users
    has_many :animal_users
  end
  
  #models/animal.rb
  class Animal &lt; ActiveRecord::Base
    has_many :users, through: :animal_users
    has_many :animal_users
  end
  
  #models/animal_user.rb
  class AnimalUser &lt; ActiveRecord::Base
    belongs_to :animal
    belongs_to :user
  end
  </code></pre>
  
  <p><strong>The <code>user</code> form</strong>: </p>
  
  <pre><code>#views/users/_form.html.erb
  &lt;%= form_for(@user) do |f| %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
  
    # Checkbox part of the form that now works!
      &lt;div&gt;
        &lt;% Animal.all.each do |animal| %&gt;
          &lt;%= check_box_tag "user[animal_ids][]", animal.id, f.object.animals.include?(animal) %&gt;
          &lt;%= animal.animal_name %&gt;
        &lt;% end %&gt;
      &lt;/div&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Strong params within the <strong><em><code>users_controller.rb</code></em></strong></p>
  
  <pre><code>  def user_params
      params.require(:user).permit(:name, animal_ids: [])
    end
  </code></pre>
  
  <p>And just for the sake of completeness, here is what is passed into the server upon form submission:</p>
  
  <pre><code>Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"xyz=", "user"=&gt;{"name"=&gt;"Neil", "animal_ids"=&gt;["1", "3"]}, "commit"=&gt;"Create User"}
   Animal Load (0.2ms)  SELECT "animals".* FROM "animals"  WHERE "animals"."id" IN (1, 3)
    (0.1ms)  begin transaction
   SQL (0.2ms)  INSERT INTO "users" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-03-26 15:29:00.478767"], ["name", "Neil"], ["updated_at", "2015-03-26 15:29:00.478767"]]
   SQL (0.1ms)  INSERT INTO "animal_users" ("animal_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["animal_id", 1], ["created_at", "2015-03-26 15:29:00.479833"], ["updated_at", "2015-03-26 15:29:00.479833"]]
   SQL (0.0ms)  INSERT INTO "animal_users" ("animal_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["animal_id", 3], ["created_at", "2015-03-26 15:29:00.480644"], ["updated_at", "2015-03-26 15:29:00.480644"]]
   SQL (0.1ms)  UPDATE "animal_users" SET "updated_at" = ?, "user_id" = ? WHERE "animal_users"."id" = 6  [["updated_at", "2015-03-26 15:29:00.481362"], ["user_id", 8]]
   SQL (0.1ms)  UPDATE "animal_users" SET "updated_at" = ?, "user_id" = ? WHERE "animal_users"."id" = 7  [["updated_at", "2015-03-26 15:29:00.482062"], ["user_id", 8]]
    (2.4ms)  commit transaction
  </code></pre>