method
batch_on_unloaded_relation
v7.1.3.4 -
Show latest stable
-
0 notes -
Class: ActiveRecord::Batches
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
batch_on_unloaded_relation(relation:, start:, finish:, load:, order:, use_ranges:, remaining:, batch_limit:)
private
Hide source
# File activerecord/lib/active_record/relation/batches.rb, line 364 def batch_on_unloaded_relation(relation,, start,, finish,, load,, order,, use_ranges,, remaining,, batch_limit)) batch_orders = build_batch_orders(order) relation = relation.reorder(batch_orders.to_h).limit(batch_limit) relation = apply_limits(relation, 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 == klass.unscoped.all.to_sql loop do if load records = batch_relation.records ids = records.map(&:id) yielded_relation = where(primary_key => ids) yielded_relation.load_records(records) elsif (empty_scope && use_ranges != false) || use_ranges ids = batch_relation.ids finish = ids.last if finish yielded_relation = apply_finish_limit(batch_relation, finish, batch_orders) yielded_relation = yielded_relation.except(:limit, :order) yielded_relation.skip_query_cache!(false) end else ids = batch_relation.ids yielded_relation = where(primary_key => ids) end break if ids.empty? primary_key_offset = ids.last raise ArgumentError.new("Primary key not included in the custom select clause") unless primary_key_offset yield yielded_relation break if ids.length < batch_limit if limit_value remaining -= ids.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) batch_relation = batch_condition(relation, primary_key, primary_key_offset, operators) end nil end