Rails 4 nested form with has_many, through and multiple select
<p>I have a problem with nested form and has_many relation. Bussiness case: there are laboratories and their suppliers. Suppliers can be shared between labs. </p>

<p><strong>Models</strong></p>

<pre><code>class Lab &lt; ActiveRecord::Base
  has_many :lab_suppliers
  has_many :suppliers, through: :lab_suppliers
  accepts_nested_attributes_for :lab_suppliers
end

class Supplier &lt; ActiveRecord::Base
  has_many :lab_suppliers
  has_many :labs, through: :lab_suppliers
  accepts_nested_attributes_for :lab_suppliers
end

class LabSupplier &lt; ActiveRecord::Base
  belongs_to :lab
  belongs_to :supplier

  accepts_nested_attributes_for :lab
  accepts_nested_attributes_for :supplier
end
</code></pre>

<p><strong>Form</strong></p>

<pre><code>&lt;%= form_for(@lab) do |f| %&gt;
  &lt;div class="field"&gt;
    &lt;%= f.label :code %&gt;&lt;br&gt;
    &lt;%= f.text_field :code %&gt;
  &lt;/div&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 class"field"&gt;
    &lt;%= fields_for :lab_suppliers do |ff| %&gt;
      &lt;%= ff.label :supplier_id %&gt;&lt;br&gt;
      &lt;%= ff.collection_select :supplier_id, Supplier.all, :id, :name,  {include_blank: true}, {:multiple =&gt; true, :class=&gt;""} %&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>Controller</strong></p>

<pre><code>class LabsController &lt; ApplicationController
  before_action :set_lab, only: [:show, :edit, :update, :destroy]

  # GET /labs/new
  def new
    @lab = Lab.new
    @lab.lab_suppliers.build
  end

  # POST /labs
  # POST /labs.json
  def create
    #raise params.inspect

    @lab = Lab.new(lab_params)

    @lab_supplier = @lab.lab_suppliers.new(params[:lab_suppliers])
    @lab_supplier.save
    @lab.save


    private

    def lab_params
      params.require(:lab).permit(:code, :name, lab_suppliers_attributes: [])
    end
end
</code></pre>

<p>Result of the inspect on params after submitting form:</p>

<p>Parameters:</p>

<pre><code>{"utf8"=&gt;"✓",
 "authenticity_token"=&gt;"...",
 "lab"=&gt;{"code"=&gt;"L01",
 "name"=&gt;"xxx"},
 "lab_suppliers"=&gt;{"supplier_id"=&gt;["",
 "1",
 "3"]},
 "commit"=&gt;"Create Lab"}
</code></pre>

<p>While submitting form I receive ActiveModel::ForbiddenAttributesError
on the line:</p>

<pre><code>@lab_supplier = @lab.lab_suppliers.new(params[:lab_suppliers])
</code></pre>

<p>What am i missing to make it work as expected?</p>