Sha256: b23b42e9c099a56b94569f6ddb3cae71ae81342422ebab41c8b511b81de011fd
Contents?: true
Size: 836 Bytes
Versions: 61
Compression:
Stored size: 836 Bytes
Contents
# frozen_string_literal: true module BetterRecord module Batches def split_batches(options = {}, &block) options.assert_valid_keys(:start, :batch_size, :preserve_order) if block_given? && arel.orders.present? && options[:preserve_order] relation = self offset = options[:start] || 0 batch_size = options[:batch_size] || 1000 total = relation.count(:*) records = relation.limit(batch_size).offset(offset).to_a while records.any? records_size = records.size block.call records break if records_size < batch_size offset += batch_size records = relation.limit(batch_size).offset(offset).to_a end nil else find_in_batches(options.except(:preserve_order), &block) end end end end
Version data entries
61 entries across 61 versions & 1 rubygems