Skip to content

Instantly share code, notes, and snippets.

@michaelbarton
Created October 2, 2008 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelbarton/14338 to your computer and use it in GitHub Desktop.
Save michaelbarton/14338 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