我有一个如下所示的数组:
@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>)
阵列的格式不正确。我需要找到一种方法来迭代键/值并提取属性,这样我就可以调用我在模型中创建的方法。有什么想法吗?
答案 0 :(得分: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