简体   繁体   中英

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. 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

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

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

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

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. 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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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