Skip to content

Instantly share code, notes, and snippets.

@flah00
Created March 27, 2013 19:40
Show Gist options
  • Save flah00/5257373 to your computer and use it in GitHub Desktop.
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...
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