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

**URL:** <https://www.ruby-forum.com/t/newb-in-need-trying-to-subtract-from-an-array-or-table/88596>\
**Category:** Rails\
**Created:** [March 12, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/newb-in-need-trying-to-subtract-from-an-array-or-table/88596 "2007-03-12T22:35:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PsionBlue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d07c76/32.png) [@PsionBlue](https://www.ruby-forum.com/u/PsionBlue)\
**Post date:** [March 12, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/newb-in-need-trying-to-subtract-from-an-array-or-table/88596/1 "2007-03-12T22:35:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PsionBlue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d07c76/32.png) [@PsionBlue](https://www.ruby-forum.com/u/PsionBlue)\
**Post date:** [March 12, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/newb-in-need-trying-to-subtract-from-an-array-or-table/88596/2 "2007-03-12T23:46:08Z")

</div>

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