title: Two levels of nested attributes and strong parameters
body: <p>I've ported a Rails app from Rails 3 to Rails 4 and most things work now, except for a problem with two levels of nested attributes:</p>
  
  <ul>
  <li>I have ProductGroups, Variants and Prices.</li>
  <li>Each ProductGroup has one or more variants. One of them is the master variant.</li>
  <li>Each variant has many prices (one for each region).</li>
  </ul>
  
  <p>I have a controller that updates ProductGroups. When the ProductGroup is updated, the master variant is updated at the same time. And prices in the master variant are also updated.</p>
  
  <p>Here's a test that describes what's expected to happend:</p>
  
  <pre><code>test "should update master variant" do
      login_as accounts(:johnny_admin)
  
      p = ProductGroup.find product_groups(:toothbrush).id
      assert_equal "10123", p.artno
      assert_equal "10123", p.master_variant.artno
  
      puts(p.master_variant.prices.to_a.to_s)
  
      post :update,
           id: product_groups(:toothbrush),
           p: 'setup',
           product_group: {
             master_variant_attributes: {
               artno: "20222",
               supplier_artno: "1010",
               prices_attributes: { "0": { price: "55", id: prices(:toothbrush_price_se).id } }
             }
           }
  
    assert_response :redirect
    assert_redirected_to edit_admin_product_group_path(p, :p =&gt; 'setup')
  
    p = ProductGroup.find product_groups(:toothbrush).id
    assert_equal "20222", p.artno
    assert_equal "20222", p.master_variant.artno
    assert_equal "1010", p.master_variant.supplier_artno
  
    price = Prices.find prices(:toothbrush_price_se).id
    assert_equal 55, price.price
  end
  </code></pre>
  
  <p>But it fails with this error:</p>
  
  <pre><code># Running:
  
  .......[#&lt;Price id: 510149407, variant_id: 630858089, region_id: 102782309, price: #&lt;BigDecimal:55d2732f50a8,'0.95E2',9(18)&gt;, created_at: "2016-12-30 11:14:28", updated_at: "2016-12-30 11:14:28"&gt;, #&lt;Price id: 524805804, variant_id: 630858089, region_id: 960235695, price: #&lt;BigDecimal:55d27339c510,'0.1E2',9(18)&gt;, created_at: "2016-12-30 11:14:28", updated_at: "2016-12-30 11:14:28"&gt;]
  E
  
  Finished in 1.279989s, 6.2501 runs/s, 20.3127 assertions/s.
  
    1) Error:
  Admin::ProductGroupsControllerTest#test_should_update_master_variant:
  ActiveRecord::RecordNotFound: Couldn't find Price with ID=510149407 for Variant with ID=
      app/controllers/admin/product_groups_controller.rb:150:in `update'
      test/functional/admin/product_groups_controller_test.rb:103:in `block in &lt;class:ProductGroupsControllerTest&gt;'
  </code></pre>
  
  <p>As you can see in the debug output, there <em>is</em> a price with ID 510149407 for that variant. And why is the ID of the variant empty?</p>
  
  <p>I'm totally stuck.</p>
  
  <p>Here's the permits for ProductGroup that I'm using:</p>
  
  <pre><code>  def product_group_params
      prices_attributes = { :prices_attributes =&gt; [ :id, :price ] }
      master_variant_attributes = { :master_variant_attributes =&gt; [
        :unit, :vat, :artno, :width, :height, :depth,
        :description, :in_stock, :in_stock_verified_at,
        :status, :supplier_id, :supplier_artno,
        :alt_supplier_id, :alt_supplier_artno,
        :supplier_price, :alt_supplier_price,
        :supplier_reduction, :alt_supplier_reduction,
        :supplier_carriage_percentage, :alt_supplier_carriage_percentage,
        :our_expenses, :percentage_markup, :taric_code_id,
        :reduction_group_id, :vendor_id, :vendor_artno, :is_expired,
        :show_price, :reorder_point,
        :place_of_storage_a, :place_of_storage_b, :place_of_storage_c,
        prices_attributes
      ] }
  
      params.require(:product_group).permit(:updated_by,
                                            :title, :description, :license_code, :fixme,
                                            master_variant_attributes,
                                            :notes, :vat, :artno, :unit,
                                            :width, :height, :depth, :in_stock, :published, :reorder_point,
                                            :current_version, :changelog, :price_by_start_cost_and_per_unit,
                                            :start_cost_variant_id, :unit_cost_variant_id,
                                            :category_ids =&gt; [])
    end
  </code></pre>
  
  <p>Here's how ProductGroup relates to the master variant:</p>
  
  <pre><code>  has_one :master_variant,
            -&gt; { where(is_master: true, deleted_at: nil) },
            :class_name =&gt; "Variant",
            :foreign_key =&gt; 'product_group_id',
            :dependent =&gt; :destroy,
            :autosave =&gt; true
    accepts_nested_attributes_for :master_variant
  </code></pre>
  
  <p>Here's how Variant relates to Prices:</p>
  
  <pre><code>  has_many :prices, -&gt; { order('region_id') }, :dependent =&gt; :destroy
    accepts_nested_attributes_for :prices
  </code></pre>
  
  <p>I will gladly post any other excerpts from the code if it is of any help, but I'm not sure what could be of interest right now.</p>
  
  <p>Any hints would be much appreciated!</p>