title: Rails 5 Assignment of _ids array fails on has_many: through association
body: <p>When upgrading from Rails 3.2 to Rails 5 I ran into this problem where a model suddenly refuses to save.</p>
  
  <p>I have 2 models: <code>ClosingDay</code> and <code>Location</code> which have a has_many :through relation via a <code>ClosingDayLocation</code> model. When creating a <code>ClosingDay</code> the user needs to select 1 or more <code>Location</code> records using checkboxes.</p>
  
  <p>However, regardless of which <code>Location</code> records I select on the form, I always get an error saving the <code>ClosingDay</code> record telling me that the associated <code>ClosingDayLocation</code> is invalid because <code>closing_day_id</code> is empty.</p>
  
  <p><strong>Models:</strong></p>
  
  <pre><code>class ClosingDay &lt; ActiveRecord::Base
    has_many :closing_day_locations, :dependent =&gt; :destroy
    has_many :locations, :through =&gt; :closing_day_locations
  end
  
  class Location &lt; ActiveRecord::Base
    has_many :closing_day_locations, :dependent =&gt; :destroy
    has_many :closing_days, :through =&gt; :closing_day_locations
  end
  
  class ClosingDayLocation &lt; ActiveRecord::Base
    belongs_to :location
    belongs_to :closing_day
  
    validates :closing_day_id, :presence =&gt; true
    validates :location_id, :presence =&gt; true
  end
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>  def create
      @closing_day = ClosingDay.build(closing_day_params)
  
      if @closing_day.save
        redirect_to(closing_days_url, :notice =&gt; 'OK')
      else
        @locations = Location.active.order(:name)
        render :action =&gt; 'new'
      end
    end
  
    private
    def closing_day_params
      params.require(:closing_day).permit(:date, :name, location_ids: [])
    end
  </code></pre>
  
  <p><strong>Form</strong></p>
  
  <pre><code>&lt;%= form_for(@closing_day) do |f| %&gt;
      &lt;%= show_errors_for(@closing_day) %&gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;%= f.label :name %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= f.text_field :name %&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;%= f.label :date %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= f.text_field :date %&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;%= label_tag 'closing_day[location_ids][]', 'Locations' %&gt;&lt;/td&gt;
          &lt;td&gt;
            &lt;%= hidden_field_tag 'closing_day[location_ids][]', nil %&gt;
            &lt;% @locations.each do |location| %&gt;
                &lt;p&gt;
                  &lt;%= label_tag do %&gt;
                      &lt;%= check_box_tag 'closing_day[location_ids][]', location.id, @closing_day.location_ids.include?(location.id) %&gt;
                      &lt;%= location.name %&gt;
                  &lt;% end %&gt;
                &lt;/p&gt;
            &lt;% end %&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td colspan="2"&gt;&lt;%= f.submit %&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>So in short, I want to create a new <code>@closing_day</code> record and assign associated (pre-existing) locations by assigning <code>Location</code> IDs using <code>@closing_day.location_ids = [3,6,9]</code> essentially. This works in Rails 3.2 but does not seem to work in Rails 5. </p>
  
  <p>What is the best way to still keep this functionality? I have also tried adding <code>accepts_nested_attributes_for :locations, :allow_destroy =&gt; true</code> and <code>accepts_nested_attributes_for :closing_day_locations, :allow_destroy =&gt; true</code> but that does not seem to work. The associated <code>ClosingDayLocation</code> models seem to be created, but the <code>closing_day_id</code> remains empty, resulting in an error.</p>