Rails extension


			
# AR extension to iterate over all records like a DB cursor
# Prevents performance problems when iterating over a table with many rows
# Usage:
# User.find_all_in_chunks(1500) { |user| user.update_attribute :name, 'jim' }
def self.find_all_in_chunks(size=1000)
  limit, offset, num_rows = size, 0, self.count

  while offset < num_rows do
    records = find :all, :limit => limit, :offset => offset
    offset += records.size
    records.each { |record| yield record }
  end
end

Crappy spec


			
describe ActiveRecord::Base, "#find_all_in_chunks" do
  it "should iterate over records like a DB cursor" do
    rows, fetched = User.count, 0

    User.find_all_in_chunks(2) do |user|
      fetched += 1
      rows.should <= rows
    end
  end
end