Skip to content

Instantly share code, notes, and snippets.

@bf4
Created March 22, 2017 18:00
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 bf4/3810befdcbb09c577b692f90064a1bcf to your computer and use it in GitHub Desktop.
Save bf4/3810befdcbb09c577b692f90064a1bcf to your computer and use it in GitHub Desktop.
ActiveRecord Rails Find Relations in Batches
# Finds and yields relations in batches. Adapted from record.find_in_batches since that yields
# arrays and we want to operate on relationships.
# Adapted from:
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/relation/batches.rb#L98-L115
def find_in_batches(target, batch_size: default_batch_size, start: nil)
relation = target
if logger && (target.arel.orders.present? || target.arel.taken.present?)
logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
end
relation = relation.reorder(target.send(:batch_order)).limit(batch_size)
records = start ? relation.where(target.table[target.primary_key].gteq(start)) : relation
while (records_size = records.length) > 0
primary_key_offset = records.last.id
fail "Primary key not included in the custom select clause" unless primary_key_offset
yield records
break if records_size < batch_size
records = relation.where(target.table[target.primary_key].gt(primary_key_offset))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment