将动态生成的属性合并到新条目中并对其值进行求和

时间:2017-03-28 16:57:39

标签: ruby-on-rails activerecord

我正在寻找一些关于如何将一些键/值对正确合并到一个单独的数据库条目并总结其值的建议。 我有一个Task,其Vendor_Upload有许多Vendor_Shipping_Logs,其中有Vendor_Shipping_Log_ProductsItem_ID。我不确定深度嵌套是否有所作为,但要在此处查看的重要值是QuantityParameters: { "task"=>{ "task_type"=>"Vendor Upload", "vendor_upload_attributes"=>{ "upload_type"=>"Warranty Orders", "vendor_shipping_logs_attributes"=>{ "1490674883303"=>{ "guest_name"=>"Martin Crane", "order_number"=>"33101", "vendor_shipping_log_products_attributes"=>{ "1490675774108"=>{ "item_id"=>"211", "quantity"=>"3" }, "1490675775147"=>{ "item_id"=>"213", "quantity"=>"6" } } }, "1490674884454"=>{ "guest_name"=>"Frasier Crane", "order_number"=>"33102", "vendor_shipping_log_products_attributes"=>{ "1490675808026"=>{ "item_id"=>"214", "quantity"=>"10" }, "1490675808744"=>{ "item_id"=>"213", "quantity"=>"1" } } }, "1490674885293"=>{ "guest_name"=>"Niles Crane", "order_number"=>"33103", "vendor_shipping_log_products_attributes"=>{ "1490675837184"=>{ "item_id"=>"211", "quantity"=>"3" } } }, "1490674886373"=>{ "guest_name"=>"Daphne Moon", "order_number"=>"33104", "vendor_shipping_log_products_attributes"=>{ "1490675852950"=>{ "item_id"=>"213", "quantity"=>"8" }, "1490675853845"=>{ "item_id"=>"214", "quantity"=>"11" } } } } } } }

目前这是参数吐出的方式:

Vendor_Shipping_Log_Product

提交后,我想合并每个唯一的Item_IDStockmovement_Batch,并将其数量汇总为新的Stockmovement作为嵌套Parameters: { "stockmovement_batch"=>{ "stockmovement_type"=>"Ecomm Order", "stockmovements_attributes"=>{ "1490676054881"=>{ "item_id"=>"211", "adjust_quantity"=>"-6" }, "1490676055897"=>{ "item_id"=>"213", "adjust_quantity"=>"-15" }, "1490676057616"=>{ "item_id"=>"214", "adjust_quantity"=>"-21" } } } } ,以使我的库存保持最新

请参阅示例patameters,了解我希望输出显示的内容:

dim numbers(5) as integer
for j = 1 to 5
    numbers(j) = j
next j

' now the shuffle
dim rnd as new Random
for i as integer = 5 to 1 step -1
    dim j as integer = rnd.Next(1, i+1)
    dim temp as integer = numbers(i)
    numbers(i) = numbers(j)
    numbers(j) = temp
next

q1 = numbers(1)
q2 = numbers(2)
q3 = numbers(3)
q4 = numbers(4)
q5 = numbers(5)

这是我能在一个简单的过程中完成的事情,还是我必须坚持以单独的形式完成每个过程?

1 个答案:

答案 0 :(得分:0)

首先,您需要将要迭代的值分开:

data = params.require("task")
                      .require("vendor_upload_attributes")
                      .require("vendor_shipping_logs_attributes")

然后拉出vendor_shipping_log_products_attributes并将其展平为一系列哈希:

logs = data.values.map do |h|
  h["vendor_shipping_log_products_attributes"].values
end.flatten


# => [{"item_id"=>"211", "quantity"=>"3"}, {"item_id"=>"213", "quantity"=>"6"}, {"item_id"=>"214", "quantity"=>"10"}, {"item_id"=>"213", "quantity"=>"1"}, {"item_id"=>"211", "quantity"=>"3"}, {"item_id"=>"213", "quantity"=>"8"}, {"item_id"=>"214", "quantity"=>"11"}]

然后我们通过创建中间哈希来合并数据,我们使用item_id作为键。

stockmovements = logs.each_with_object({}) do |hash, memo|
  id = hash["item_id"]
  memo[id] ||= []
  memo[id].push(hash["quantity"].to_i)
end

# => {"211"=>[3, 3], "213"=>[6, 1, 8], "214"=>[10, 11]}

然后我们可以映射结果并对值求和:

stockmovements.map do |(k,v)|
  {
     item_id: k,
     adjust_quantity: 0 - v.sum
  }
end

# => [{:item_id=>"211", :adjust_quantity=>-6}, {:item_id=>"213", :adjust_quantity=>-15}, {:item_id=>"214", :adjust_quantity=>-21}]