"324", "product_id"=>"1", "qt...">

Rails 3.2 iterating through an array

I have an array that looks like this:

@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"} ] 

I want to end up doing something like this

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

I get the following error

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

The array is not quite in the correct format for this. I need to find a way to iterate over the key / values ​​and retrieve the attributes so that I can call the method that I created in the model. Any ideas?

+4
source share
1 answer

Check out the following code.

  @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 
+8
source

All Articles