title: Rails Nested attributes with check_box loop
body: <p>So I have a categories model</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
  has_many :provider_categories
  has_many :providers, :through =&gt; :provider_categories
  end
  </code></pre>
  
  <p>and a provider model</p>
  
  <pre><code>class Provider &lt; ActiveRecord::Base
  has_many :provider_categories
  has_many :categories, :through =&gt; :provider_categories
  
  accepts_nested_attributes_for :provider_categories, :allow_destroy =&gt; true
  end
  </code></pre>
  
  <p>here is my provider_category model</p>
  
  <pre><code>class ProviderCategory &lt; ActiveRecord::Base
  belongs_to :provider
  belongs_to :category
  end
  </code></pre>
  
  <p>I'm trying to create a nested form to include the categories into the provider form</p>
  
  <pre><code>= form_for @provider do |f|
    ...
    -@categories.each.with_index do |category,index|
      =f.fields_for :provider_category, category do |pl|
        =pl.check_box :category_id
        =pl.label category.name
        =pl.hidden_field :provider_id, value: @provider.id
  </code></pre>
  
  <p>The checkbox is not cooperating when I load the page I get</p>
  
  <pre><code>undefined method `category_id' for #&lt;Category:0x0000010184ff60&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>if I change the :category_id to id it works but the html outpuy looks like this</p>
  
  <pre><code>&lt;input name="provider[provider_category][id]" type="hidden" value="0"&gt;&lt;input checked="checked" id="provider_provider_category_id" name="provider[provider_category][id]" type="checkbox" value="1"&gt;
  &lt;label for="provider_provider_category_Acupuncture"&gt;Acupuncture&lt;/label&gt;
  &lt;input id="provider_provider_category_provider_id" name="provider[provider_category][provider_id]" type="hidden" value="1"&gt;
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I ended up using check_box_tag</p>
  
  <pre><code>-@categories.each.with_index do |category|
    =f.fields_for :provider_category, category do |pl|
      .li
        =check_box_tag "provider[provider_category][category_id]", category.id
        =pl.label category.name
        =pl.hidden_field :provider_id, value: @provider.id
  </code></pre>
  
  <p>Now the html output seems to be correct but it's not saving it. When I select multiple items this is the hash from saving</p>
  
  <pre><code>"provider"=&gt;{"name"=&gt;"Mr. Awesome", "phone"=&gt;"999-999-9999", "email"=&gt;"awesome@awesome.com", "address"=&gt;"awesome land", "provider_category"=&gt;{"category_id"=&gt;"3", "provider_id"=&gt;"1"}}, "commit"=&gt;"Save", "id"=&gt;"1"}
  </code></pre>
  
  <p>I did select 3 options but only the last one was added to the hash</p>
  
  <p>I also added the provider categories in the strong params of provider controller</p>
  
  <pre><code>params.require(:provider).permit(..., :provider_categories =&gt; [:category_id, :provider_id])
  </code></pre>
  
  <p>Thanks for the help in advance</p>