Sha256: f50f632a834e4e48123a3d35f909ed35cfb923abd02d71ec10fb427e0a8ceae9

Contents?: true

Size: 775 Bytes

Versions: 2

Compression:

Stored size: 775 Bytes

Contents

require "activerecord/pluck_in_batches/version"
require "active_record/relation"

module InBatches
  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
end

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

ActiveRecord::Relation.send :include, InBatches if Rails.version < "5.0.0"
ActiveRecord::Relation.send :include, PluckInBatches

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-pluck_in_batches-0.2.1 lib/activerecord/pluck_in_batches.rb
activerecord-pluck_in_batches-0.2.0 lib/activerecord/pluck_in_batches.rb