Iterating over a collection, three at a time

Hi all,

The subject sums up what I want to do in a nutshell. I have a collection
of ActiveRecord objects (@galleries), and I want to generate from it
another collection of arrays, each three elements long. This is simply
so I can display preview pictures from all my @galleries in a three
column table.

What I want to end up with is something like this that I can iterate
over to build my table:

@collected -> [[gallery1, gallery2, gallery3],
               [gallery4, gallery5, gallery6]]


I cannot for the life of me figure out a clean way to do it. Any
hints or prods in the right direction would be great :wink:

Many thanks,


Have a look at in_groups_of:

HI Dan

Did you try “in_groups_of”

Aha! That looks just the thing! Thanks (to Greg, too) for the pointer.
I should have known Rails could do it for me!