Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process a large ActiveRecord result set in groups

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!

like image 278
bloudermilk Avatar asked Nov 30 '22 19:11

bloudermilk


1 Answers

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.

like image 158
Chandra Patni Avatar answered Dec 06 '22 17:12

Chandra Patni