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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With