加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Rails 3.2迭代数组

发布时间:2020-12-17 03:46:44 所属栏目:百科 来源:网络整理
导读:我有一个看起来像这样的数组: @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"=
我有一个看起来像这样的数组:

@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>)

这个数组的格式不是很合适.我需要找到一种方法来迭代键/值并提取属性,这样我就可以调用我在模型中创建的方法.有任何想法吗?

解决方法

检查以下代码.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读