Skip to content

Instantly share code, notes, and snippets.

@bonkydog
Forked from michaelbarton/chunky_each.rb
Created November 10, 2009 19:48
Show Gist options
  • Save bonkydog/231205 to your computer and use it in GitHub Desktop.
Save bonkydog/231205 to your computer and use it in GitHub Desktop.
# Example activerecord interator based on using an indexed 'id' column.
# See http://schuerig.de/michael/blog/index.php/2007/02/03/ar-enumerable/
# and http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord
class << ActiveRecord::Base
def each(chunk_size=1000)
(0..self.last.id / chunk_size).each do |offset|
self.find(:all,
:limit => chunk_size,
:conditions => ["id > ?", offset * chunk_size]).each do |i|
yield i
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment