Repository: rails/rails
File: activerecord/lib/active_record/relation/batches.rb (Ruby)

Code before:
        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)
            ids_size = ids.size
            ids_last = ids.last
            yielded_relation = where(primary_key => ids)
            yielded_relation.load_records(records)
          elsif (empty_scope && use_ranges != false) || use_ranges
            # Efficiently peak at the last id for the next batch using offset and limit.
            ids_size = batch_limit
            ids_last = batch_relation.offset(batch_limit - 1).limit(1).ids.first
            # If the last id is not found using offset, there is at most one more batch of size < batch_limit.
            # We retry getting the list of ids so that we have the exact size and last id.
            unless ids_last
              ids = batch_relation.ids
              ids_size = ids.size
              ids_last = ids.last
            end
            if ids_last
              yielded_relation = apply_finish_limit(batch_relation, ids_last, batch_orders)
              yielded_relation = yielded_relation.except(:limit, :order)
              yielded_relation.skip_query_cache!(false)
            end
          else
            ids = batch_relation.ids
            ids_size = ids.size
            ids_last = ids.last
            yielded_relation = where(primary_key => ids)
          end

          break if ids_size == 0

          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_size < batch_limit

          if limit_value
            remaining -= ids_size

            if remaining == 0
              # Saves a useless iteration when the limit is a multiple of the
              # batch size.

Review comment (suggestion):
@maximerety good stuff removing the confusing second introduction of `finish` in this scope. thank you ❤️ 

Code after:
      end

      def act_on_ignored_order(error_on_ignore)
        raise_error = (error_on_ignore.nil? ? ActiveRecord.error_on_ignored_order : error_on_ignore)

        if raise_error
          raise ArgumentError.new(ORDER_IGNORE_MESSAGE)
        elsif model.logger
          model.logger.warn(ORDER_IGNORE_MESSAGE)
        end
      end

      def batch_on_loaded_relation(relation:, start:, finish:, cursor:, order:, batch_limit:)
        records = relation.to_a
        order = build_batch_orders(cursor, order).map(&:second)

        if start || finish
          records = records.filter do |record|
            values = record_cursor_values(record, cursor)

            (start.nil? || compare_values_for_order(values, Array(start), order) >= 0) &&
              (finish.nil? || compare_values_for_order(values, Array(finish), order) <= 0)
          end
        end

        records.sort! do |record1, record2|
          values1 = record_cursor_values(record1, cursor)
          values2 = record_cursor_values(record2, cursor)
          compare_values_for_order(values1, values2, order)
        end

        records.each_slice(batch_limit) do |subrecords|
          subrelation = relation.spawn
          subrelation.load_records(subrecords)

          yield subrelation
        end

        nil
      end

      def record_cursor_values(record, cursor)
        record.attributes.slice(*cursor).values
      end

      # This is a custom implementation of `<=>` operator,
      # which also takes into account how the collection will be ordered.
      def compare_values_for_order(values1, values2, order)
        values1.each_with_index do |element1, index|
          element2 = values2[index]
          direction = order[index]