提交 b37bd3d5 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #25941 from kamipo/finder_methods_uses_load_target

`FinderMethods` uses `records` (`load_target`) when `loaded?` is true
......@@ -29,7 +29,7 @@ module Associations
# instantiation of the actual post records.
class CollectionProxy < Relation
delegate(*(ActiveRecord::Calculations.public_instance_methods - [:count]), to: :scope)
delegate :find_nth, :exists?, :update_all, :arel, to: :scope
delegate :exists?, :update_all, :arel, to: :scope
def initialize(klass, association) #:nodoc:
@association = association
......
......@@ -514,7 +514,7 @@ def find_some_ordered(ids)
def find_take
if loaded?
@records.first
records.first
else
@take ||= limit(1).records.first
end
......@@ -531,7 +531,7 @@ def find_nth(index, offset = nil)
MSG
end
if loaded?
@records[index]
records[index]
else
offset ||= offset_index
@offsets[offset + index] ||= find_nth_with_limit_and_offset(index, 1, offset: offset).first
......@@ -557,7 +557,7 @@ def find_nth_with_limit(index, limit)
def find_nth_from_last(index)
if loaded?
@records[-index]
records[-index]
else
relation = if order_values.empty? && primary_key
order(arel_attribute(primary_key).asc)
......@@ -578,7 +578,7 @@ def find_nth_from_last(index)
def find_nth_with_limit_and_offset(index, limit, offset:) # :nodoc:
if loaded?
@records[index, limit]
records[index, limit]
else
index += offset
find_nth_with_limit(index, limit)
......
......@@ -254,6 +254,8 @@ def test_scoped_allows_conditions
test "first! works on loaded associations" do
david = authors(:david)
assert_equal david.posts.first, david.posts.reload.first!
assert david.posts.loaded?
assert_no_queries { david.posts.first! }
end
def test_reset_unloads_target
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册