Module: PluckInBatches

Defined in:
lib/activerecord/pluck_in_batches.rb

Instance Method Summary collapse

Instance Method Details

#in_batches(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/activerecord/pluck_in_batches.rb', line 12

def in_batches(options = {})
  options.assert_valid_keys(:batch_size)

  batch_size = options[:batch_size] || 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

#pluck_in_batches(*args, &block) ⇒ Object



5
6
7
8
9
10
# File 'lib/activerecord/pluck_in_batches.rb', line 5

def pluck_in_batches(*args, &block)
  options = args.extract_options!
  in_batches(options) do |batch|
    batch.pluck(*args).each(&block)
  end
end