ActiveRecord::Base#find_in_batches.
This lets you iterate over all the records in cursor-like fashion (only retrieving a set number of records at a time into the memory):
@Subscription.find_in_batches(batch_size => 100 ) { |subs| subs.each { |s| ... } }@
iterate over all subscriptions in chunks of 100
find_in_batches supports scopes
@class Subscription < ActiveRecord::Base
scope :expired, :conditions => { :expired => true }
end
@
@Subscription.expired.find_in_batches(:batch_size => 100 ) { |subs| ... }@
_Disadvantage: _
You can’t specify :order or :limit in the options.
Where to use it:
Large Dataset, where you need to loop through all records.
Effectiveness:
If done using a normal find the full result-set will be loaded into memory and could cause problems.
With Batch only 100 * (each result-object size) will be loaded into memory.