My Profile Photo

rubycoloredglasses


I'm Jason, a web applications developer in the San Francisco Bay area.


Using Find Each to Process Batches

I just found out that there is a find_each method provided by ActiveRecord which loops through an array of models that are retrieved in batches of 1000 at a time.

The find is performed by find_in_batches with a batch size of 1000 (or as specified by the :batch_size option).

User.find_each(:start => 2000, :batch_size => 5000) do |user|
  user.do_something
end