Newb in need - trying to subtract from an array? or table?

Here is my problem in english.

I have an INVOICE table which has orders with a quantity. I need to
take those orders out and make new 12 pack boxes from the original
invoice. For example:

Invoice 1 is an order for 3 widgets

Invoice 2 is an order for 12 widgets

Invoice 3 is an order for 6 widgets

What should happen is the 3 widgets from order one is combined with 9
widgets in order two to make a packing slip for 12 widgets. The
remaining total is less that 12 so nothing happens with the rest. The
transaction would need to be logged as weel but that is another issue.

I was initially planning on just turning every invoice into rows and
the just grabbing the top 12 or with a do 12 times or whatever. That
way I get only what I need. I am just wondering if there is a way
without creating so many records in the database.

Thanks in advance!

brendan

you could combine the arrays of widgets into one array then use the
rails helper in_groups_of to go through them

a = [1,2,3]
=> [1, 2, 3]

b = [4,5,6,7,8]
=> [4, 5, 6, 7, 8]

c = [9,10,11,12]
=> [9, 10, 11, 12]

n = a + b + c
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

n.in_groups_of(7)
=> [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, nil, nil]]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs