Created
March 27, 2013 19:40
-
-
Save flah00/5257373 to your computer and use it in GitHub Desktop.
Rails `find_in_batches` solves a vary particular problem, by checking your model's primary keys. If your primary key is never repeated you can use it with ease. If, on the other hand, your primary key repeats, you've got a problem on your hands... You can go from the other side of the relationship or you can brute force it...
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
module ActiveRecord | |
module Batches | |
def brute_batch(options={}) | |
options.reverse_merge!(batch_size: 1000, offset: 0) | |
offset = options[:offset].to_i | |
while true do | |
results = offset(offset).limit(options[:batch_size]) | |
offset += results.count | |
yield results | |
break unless results.any? | |
end | |
offset | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment