Module: InBatches

Defined in:
lib/activerecord/pluck_in_batches.rb

Instance Method Summary collapse

Instance Method Details

#in_batches(of: 1000) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/activerecord/pluck_in_batches.rb', line 5

def in_batches(of: 1000)
  batches = (count / batch_size.to_f).ceil

  relation = reorder("#{quoted_table_name}.#{quoted_primary_key} ASC").limit(batch_size)

  if block_given?
    batches.times do |i|
      yield relation.offset(i * batch_size)
    end
  else
    batches.times.map { |i| relation.offset(i * batch_size) }
  end
end