module Riveter::CoreExtensions::BatchFinderSupport

Public Instance Methods

find_each_with_order(options={}) { |record| ... } click to toggle source

finds each record in batches while preserving the specified order of the relation

# File lib/riveter/core_extensions.rb, line 183
def find_each_with_order(options={})
  return to_enum(__method__, options) unless block_given?
  find_in_batches_with_order(options) do |records|
    records.each { |record| yield record }
  end
end
find_in_batches_with_order(options={}) { |records| ... } click to toggle source

finds each record in batches while preserving the specified order of the relation NOTE: any limit() on the query is overridden with the batch size

# File lib/riveter/core_extensions.rb, line 193
def find_in_batches_with_order(options={})
  return to_enum(__method__, options) unless block_given?
  options.assert_valid_keys(:batch_size)

  relation = self

  start = 0
  batch_size = options.delete(:batch_size) || 1000

  relation = relation.limit(batch_size)
  records = relation.offset(start).to_a

  while records.any?
    records_size = records.size

    yield records

    break if records_size < batch_size

    # get the next batch
    start += batch_size
    records = relation.offset(start + 1).to_a
  end
end