instance method
batch_on_unloaded_relation
Ruby on Rails 8.0.4
Since v7.1.6 PrivateSignature
batch_on_unloaded_relation(relation:, start:, finish:, load:, cursor:, order:, use_ranges:, remaining:, batch_limit:)
No documentation comment.
Parameters
-
relationkeyreq -
startkeyreq -
finishkeyreq -
loadkeyreq -
cursorkeyreq -
orderkeyreq -
use_rangeskeyreq -
remainingkeyreq -
batch_limitkeyreq
Source
# File activerecord/lib/active_record/relation/batches.rb, line 426
def batch_on_unloaded_relation(relation:, start:, finish:, load:, cursor:, order:, use_ranges:, remaining:, batch_limit:)
batch_orders = build_batch_orders(cursor, order)
relation = relation.reorder(batch_orders.to_h).limit(batch_limit)
relation = apply_limits(relation, cursor, start, finish, batch_orders)
relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
batch_relation = relation
empty_scope = to_sql == model.unscoped.all.to_sql
loop do
if load
records = batch_relation.records
values = records.pluck(*cursor)
yielded_relation = where(cursor => values)
yielded_relation.load_records(records)
elsif (empty_scope && use_ranges != false) || use_ranges
values = batch_relation.pluck(*cursor)
finish = values.last
if finish
yielded_relation = apply_finish_limit(batch_relation, cursor, finish, batch_orders)
yielded_relation = yielded_relation.except(:limit, :order)
yielded_relation.skip_query_cache!(false)
end
else
values = batch_relation.pluck(*cursor)
yielded_relation = where(cursor => values)
end
break if values.empty?
if values.flatten.any?(nil)
raise ArgumentError, "Not all of the batch cursor columns were included in the custom select clause "\
"or some columns contain nil."
end
yield yielded_relation
break if values.length < batch_limit
if limit_value
remaining -= values.length
if remaining == 0
# Saves a useless iteration when the limit is a multiple of the
# batch size.
break
elsif remaining < batch_limit
relation = relation.limit(remaining)
end
end
batch_orders_copy = batch_orders.dup
_last_column, last_order = batch_orders_copy.pop
operators = batch_orders_copy.map do |_column, order|
order == :desc ? :lteq : :gteq
end
operators << (last_order == :desc ? :lt : :gt)
cursor_value = values.last
batch_relation = batch_condition(relation, cursor, cursor_value, operators)
end
nil
end
Defined in activerecord/lib/active_record/relation/batches.rb line 426
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Batches