views:

251

answers:

2

I'm wondering if there is a way to take an array of ActiveRecord results (or any array, for that matter) and process it in groups of 25 or so. Something like this:

User.all.each(25) do |group|
    # Some code that works with this group of 25
end

I'm just looking to avoid doing multiple successive database queries. Thanks!

A: 

You're probably looking for

Array#in_groups_of

http://weblog.rubyonrails.org/2006/3/1/new-in-rails-enumerable-group%5Fby-and-array-in%5Fgroups%5Fof

Matt Rogish
This will fetch the entire collection and store it all in memory. Bad idea.
Ryan Bigg
Depends on how many rows and how big. There is a trade-off between query/network efficiency and memory use of the ruby process. Only the original question asker has the knowledge to say which applies to their problem
Matt Rogish
Ryan is right. `AR::Base#find_each` or `AR::Base#find_in_batches` is the solution.
Steve Graham
If you're `find(:all)`ing a 10mil row table it's going to store it all in memory. Using in_groups_of will only accentuate that problem.
Ryan Bigg
+10  A: 

Rails 2.3 have this feature. You can specify batch_size parameter.

User.find_in_batches(:batch_size =>25) do |group|
    # Some code that works with this group of 25
end

You can find a good tutorial here. Note that Rails will issue query for every 25 records. This is helpful to keep memory low if you are processing large number of records. If you want to split the results in multiple arrays, then you can use in_groups_of as suggested by Matt.

Chandra Patni
I had initially picked you answer but wound up going with Matt Rogish's because it avoids doing multiple queries.
Bloudermilk
I urge you to reconsider Bloudermilk. If you have a lot of records it's not only going to take a *lot* of time to fetch them, but also use up a lot of memory which could potentially make your server unresponsive. Processing them in small batches is much, much, MUCH better than fetching them all at once.
Ryan Bigg