Created
October 2, 2008 11:30
-
-
Save michaelbarton/14338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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