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

etc

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,

Dan

Have a look at in_groups_of:

http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/Array/Grouping.html#M000636

HI Dan

Did you try “in_groups_of”

http://www.noobkit.com/show/ruby/rails/rails-edge/activesupport-edge/activesupport/coreextensions/array/grouping/in_groups_of.html

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

Cheers,

Dan