Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
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")
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 =
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment