Module: Riveter::CoreExtensions::BatchFinderSupport

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::Relation
Defined in:
lib/riveter/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#find_each_with_order(options = {}) ⇒ Object

finds each record in batches while preserving the specified order of the relation



183
184
185
186
187
188
# File 'lib/riveter/core_extensions.rb', line 183

def find_each_with_order(options={})
  return to_enum(__method__, options) unless block_given?
  find_in_batches_with_order(options) do |records|
    records.each { |record| yield record }
  end
end

#find_in_batches_with_order(options = {}) ⇒ Object

finds each record in batches while preserving the specified order of the relation NOTE: any limit() on the query is overridden with the batch size



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/riveter/core_extensions.rb', line 193

def find_in_batches_with_order(options={})
  return to_enum(__method__, options) unless block_given?
  options.assert_valid_keys(:batch_size)

  relation = self

  start = 0
  batch_size = options.delete(:batch_size) || 1000

  relation = relation.limit(batch_size)
  records = relation.offset(start).to_a

  while records.any?
    records_size = records.size

    yield records

    break if records_size < batch_size

    # get the next batch
    start += batch_size
    records = relation.offset(start + 1).to_a
  end
end