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

Code before:
        if loaded?
          records[-index]
        else
          relation = ordered_relation

          if relation.order_values.empty? || relation.has_limit_or_offset?
            relation.records[-index]
          else
            relation.reverse_order.offset(index - 1).first
          end
        end
      end

      def find_last(limit)
        limit ? records.last(limit) : records.last
      end

      def ordered_relation
        if order_values.empty?
          if !_order_columns.empty?
            return order(_order_columns.map { |column| table[column].asc })
          end

          raise OrderError, <<~MSG.squish
            Relation has no current order, and #{model} has no order columns to use as a default.
            At least one of `implicit_order_column`, `query_constraints`, or `primary_key` must be set on the model.
          MSG
        else
          self
        end
      end

      def _order_columns
        oc = []

        oc << model.implicit_order_column if model.implicit_order_column
        oc << model.query_constraints_list if model.query_constraints_list

        if model.primary_key && model.query_constraints_list.nil?
          oc << model.primary_key
        end

        oc.flatten.uniq.compact
      end
  end
end


Review comment (suggestion):
I think `must be set` isn't quite correct, because you could also specify an `order` on the relation? (same concern with the change to the other error)

Code after:
        if loaded?
          records[-index]
        else
          relation = ordered_relation

          if relation.order_values.empty? || relation.has_limit_or_offset?
            relation.records[-index]
          else
            relation.reverse_order.offset(index - 1).first
          end
        end
      end

      def find_last(limit)
        limit ? records.last(limit) : records.last
      end

      def ordered_relation
        if order_values.empty?
          if !_order_columns.empty?
            return order(_order_columns.map { |column| table[column].asc })
          end

          if ActiveRecord.raise_on_missing_required_finder_order_columns
            raise MissingRequiredOrderError, <<~MSG.squish
              Relation has no order values, and #{model} has no order columns to use as a default.
              Set at least one of `implicit_order_column`, `query_constraints` or `primary_key` on
              the model when no `order `is specified on the relation.
            MSG
          else
            ActiveRecord.deprecator.warn(<<~MSG)
              Calling order dependent finder methods (e.g. `#first`, `#second`) without `order` values on the relation,
              and on a model (#{model}) that does not have any order columns (`implicit_order_column`, `query_constraints`,
              or `primary_key`) to fall back on is deprecated and will raise `ActiveRecord::MissingRequiredOrderError`
              in Rails 8.2.
            MSG

            self
          end
        else
          self
        end
      end

      def _order_columns
        columns = Array(model.implicit_order_column)

        return columns.compact if columns.length.positive? && columns.last.nil?

        columns += Array(model.query_constraints_list || model.primary_key)