Module: EachBatch::ActiveRecord::Relation

Defined in:
lib/each_batch/active_record/relation.rb

Instance Method Summary collapse

Instance Method Details

#each_batch(of: 1000, load: false, order: :asc, keys: [primary_key]) {|x| ... } ⇒ EachBatch::BatchEnumerator

Process records in batches. Optionally specify the keys by which to calculate the batch offsets.

Parameters:

  • of (Integer) (defaults to: 1000)

    1000 The batch size

  • load (Boolean) (defaults to: false)

    false Whether the batch records should be loaded

  • order (Symbol, String) (defaults to: :asc)

    :asc The order of processing

  • keys (Array<String, Symbol>) (defaults to: [primary_key])

    The keys used for the ordering

Yield Parameters:

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/each_batch/active_record/relation.rb', line 19

def each_batch(of: 1000, load: false, order: :asc, keys: [primary_key], &block)
  batch_enumerator = ::EachBatch::BatchEnumerator.new(
    self,
    of: of,
    load: load,
    order: order,
    keys: keys
  )

  return batch_enumerator unless block_given?

  batch_enumerator.each(&block)
end