两级嵌套属性和强参数

时间:2016-12-30 11:21:53

标签: ruby-on-rails ruby-on-rails-4 strong-parameters

我已经将Rails 3中的Rails应用程序移植到Rails 4,现在大多数工作都有效,除了两级嵌套属性的问题:

  • 我有产品组,变体和价格。
  • 每个ProductGroup都有一个或多个变体。其中一个是主变体。
  • 每个变体都有很多价格(每个地区一个)。

我有一个更新ProductGroups的控制器。更新ProductGroup时,将同时更新主变体。并且主变量中的价格也会更新。

这是一个描述预期会发生什么的测试:

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 => '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

但它失败了这个错误:

# Running:

.......[#<Price id: 510149407, variant_id: 630858089, region_id: 102782309, price: #<BigDecimal:55d2732f50a8,'0.95E2',9(18)>, created_at: "2016-12-30 11:14:28", updated_at: "2016-12-30 11:14:28">, #<Price id: 524805804, variant_id: 630858089, region_id: 960235695, price: #<BigDecimal:55d27339c510,'0.1E2',9(18)>, created_at: "2016-12-30 11:14:28", updated_at: "2016-12-30 11:14:28">]
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 <class:ProductGroupsControllerTest>'

正如您在调试输出中看到的那样,该变体的ID为510149407的价格。为什么变体的ID是空的?

我完全陷入了困境。

以下是我使用的ProductGroup许可证:

  def product_group_params
    prices_attributes = { :prices_attributes => [ :id, :price ] }
    master_variant_attributes = { :master_variant_attributes => [
      :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 => [])
  end

以下是ProductGroup与主变体的关系:

  has_one :master_variant,
          -> { where(is_master: true, deleted_at: nil) },
          :class_name => "Variant",
          :foreign_key => 'product_group_id',
          :dependent => :destroy,
          :autosave => true
  accepts_nested_attributes_for :master_variant

以下是Variant与价格的关系:

  has_many :prices, -> { order('region_id') }, :dependent => :destroy
  accepts_nested_attributes_for :prices

如果有任何帮助,我很乐意发布代码中的任何其他摘录,但我不确定现在可能有什么好处。

任何提示都会非常感激!

0 个答案:

没有答案