简体   繁体   English

将项目分类到运输包装中,Rails 3中具有belongs_to关系的模型

[英]Sorting items into shipping packages, Models with belongs_to relationship in Rails 3

I'm trying to sort variations into boxes to calculate shipping via the USPS API. 我正在尝试将变体分类到框中,以通过USPS API计算运费。 Each box can contain a specific amount of variations before it needs to be places in a new box. 每个盒子可以包含特定数量的变体,然后才需要放置在新盒子中。 Each variation belongs to a ShippingBox 每个变体都属于一个ShippingBox

This seems like a very specific question for which I'm sure there's a vague (but good) answer floating around on the internet. 这似乎是一个非常具体的问题,我敢肯定,互联网上有一个模糊(但很好)的答案。 I'm unsure of what to search for to help me on my way. 我不确定要寻找什么对我有帮助。

Variation 变异

class Variation < ActiveRecord::Base
    belongs_to :shipping_box
end

ShippingBox 运送箱

class ShippingBox < ActiveRecord::Base
   attr_accessible :name, :weight, :box_length, :box_height, :box_depth, :maximum_quantity
end

This is some pretty worthless code right now, but I feel like I'm on the right track 目前这是一些非常毫无价值的代码,但我感觉自己走对了

packages = o.variations.reduce([]) do |boxes, item|
    this_box_type = item.shipping_type
    boxes << [this_box_type.name] if boxes.empty?

    boxes.each_with_index do |box, index|
        if boxes[index].length < this_box_type.maximum_quantity
            boxes[index] << item
        end
    end
end

results 结果

=> [["25x6x6", #<Variation id: 20, cart_id: 4, order_id: nil, quantity: 2, created_at: "2013-11-26 17:49:53", updated_at: "2013-11-26 17:50:08">]] 

I find myself stuck not being able to set 2-dimensional arrays, or knowing how to use a variable as a hash key and set the value. 我发现自己无法设置二维数组,或者不知道如何使用变量作为哈希键并设置值。 I think, ideally the result would be something like: 我认为,理想的结果是:

[["25x6x6", [#<Variation id: 20, cart_id: 4, order_id: nil, quantity: 1, created_at: "2013-11-26 17:49:53", updated_at: "2013-11-26 17:50:08">, #<Variation id: 20, cart_id: 4, order_id: nil, quantity: 1, created_at: "2013-11-26 17:49:53", updated_at: "2013-11-26 17:50:08">]], ["30x5x5", [#<Variation id: 21, cart_id: 4, order_id: nil, quantity: 1, created_at: "2013-11-26 17:49:53", updated_at: "2013-11-26 17:50:08">]]]

order.shipping_types_in_order order.shipping_types_in_order

[{:shipping_id=>4}, {:shipping_id=>2}]

order.flat_packing_list order.flat_packing_list

[#<Variation id: 20, cart_id: 4, order_id: nil, quantity: 1, created_at: "2013-11-26 17:49:53", updated_at: "2013-11-28 14:33:35">, #<Variation id: 24, cart_id: 4, order_id: nil, quantity: 1, created_at: "2013-11-28 14:00:59", updated_at: "2013-11-28 14:00:59">, #<Variation id: 25, cart_id: 4, order_id: nil, quantity: 1, created_at: "2013-11-28 14:33:32", updated_at: "2013-11-28 14:33:32">]

Update 1 更新1

packages = order.shipping_types_in_order.reduce([]) do |items, boxes|
    temp = []
    puts boxes[:shipping_id]
    order.flat_packing_list.reduce([]) do |x, item|
        if boxes[:shipping_id] == [item.shipping_type.id]
            temp << item
        end
    end
    boxes[:shipping_id] << temp
end

shipping_types_in_order is an array of all unique shipping boxes available for that order. shipping_types_in_order是该订单可用的所有唯一装运箱的数组。 Looping through my order's available shipping boxes and checking the items against that seems to be a much simpler logic, but I'm still at a loss. 遍历我订单的可用运送箱并检查该物品似乎是一个简单得多的逻辑,但我仍然茫然。

Update 2 更新2

items = Array.new
packages = order.shipping_types_in_order.reduce([]) do |x, box|
    pack_list = { box: box[:shipping_id], packages: [] }
    total = order.flat_packing_list.reduce([]) do |y, item|
        puts "shipping_id: #{item.variation.shipping_type.id}"
        if box[:shipping_id] == item.variation.shipping_type.id
            pack_list[:packages] << item
        end
    end
    items << pack_list
end

I think this is really quite close now 我觉得现在真的很接近

我认为应该

boxes[index] << [item]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM