Skip to content

Instantly share code, notes, and snippets.

@tom-lord
Created October 12, 2018 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom-lord/1a89a691f295bce24621ec267c144f9a to your computer and use it in GitHub Desktop.
Save tom-lord/1a89a691f295bce24621ec267c144f9a to your computer and use it in GitHub Desktop.
# Snippets taken from:
# https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/activerecord/lib/active_record/relation/batches.rb#L201
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
relation = self
# ...
batch_limit = of
# ...
relation = relation.reorder(batch_order).limit(batch_limit)
relation = apply_limits(relation, start, finish)
# ...
end
def batch_order
arel_attribute(primary_key).asc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment