4

我有一个看起来像这样的数组:

@shipment_products
[
  {"old_qty_shipped"=>"324", "product_id"=>"1", "qty_shipped"=>"12443"}
  {"old_qty_shipped"=>"4343423", "product_id"=>"3", "qty_shipped"=>"321344"}
  {"old_qty_shipped"=>"23", "product_id"=>"4", "qty_shipped"=>"321"}
]

我希望最终能够做这样的事情

@shipment_products.each do |p|
  Product.adjust_qtys(p.old_qty_shipped, p.qty_shipped, p.product_id)
end

我收到以下错误

NoMethodError (undefined method `qty_shipped' for #<ActiveSupport::HashWithIndifferentAccess:0x007f>)

该数组的格式不完全适合执行此操作。我需要找到一种能够遍历键/值并提取属性的方法,这样我就可以调用我在模型中创建的方法。有任何想法吗?

4

1 回答 1

10

检查以下代码。

   @shipment_products = [ {"old_qty_shipped"=>"324", "product_id"=>"1", "qty_shipped"=>"12443"}, {"old_qty_shipped"=>"4343423", "product_id"=>"3", "qty_shipped"=>"321344"} , {"old_qty_shipped"=>"23", "product_id"=>"4", "qty_shipped"=>"321"}]

    @shipment_products.each do |p|
      Product.adjust_qtys(p['old_qty_shipped'], p['qty_shipped'], p['product_id'])
    end
于 2012-07-06T04:56:26.163 回答