Method: OccamsRecord::Query#find_in_batches

Defined in:
lib/occams-record/query.rb

#find_in_batches(batch_size: 1000, use_transaction: true, append_order_by: nil) {|OccamsRecord::Results::Row| ... } ⇒ Enumerator

Load records in batches of N and yield each batch to a block if given. If no block is given, returns an Enumerator.

NOTE Unlike ActiveRecord’s find_each, ORDER BY is respected. The primary key will be appended to the ORDER BY clause to help ensure consistent batches. Additionally, it will be run inside of a transaction.

Parameters:

  • batch_size (Integer) (defaults to: 1000)
  • use_transaction (Boolean) (defaults to: true)

    Ensure it runs inside of a database transaction

  • append_order_by (String) (defaults to: nil)

    Append this column to ORDER BY to ensure consistent results. Defaults to the primary key. Pass false to disable.

Yields:

Returns:

  • (Enumerator)

    will yield each batch



204
205
206
207
208
209
210
211
212
213
# File 'lib/occams-record/query.rb', line 204

def find_in_batches(batch_size: 1000, use_transaction: true, append_order_by: nil)
  enum = Batches::OffsetLimit::Scoped
    .new(model, scope, use: @use, query_logger: @query_logger, eager_loaders: @eager_loaders)
    .enum(batch_size: batch_size, use_transaction: use_transaction, append_order_by: append_order_by)
  if block_given?
    enum.each { |batch| yield batch }
  else
    enum
  end
end