Skip to content

Commit

Permalink
Use already-loaded relation contents in #sole
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewd authored and Schwad committed Jan 23, 2025
1 parent d604976 commit 5e361bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def take!
#
# Product.where(["price = %?", price]).sole
def sole
found, undesired = limit(2)
found, undesired = take(2)

if found.nil?
raise_record_not_found_exception!
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,15 @@ def test_sole_failing_many
end
end

def test_sole_on_loaded_relation
relation = Topic.where("title = 'The First Topic'").load
expected_topic = topics(:first)

assert_no_queries do
assert_equal expected_topic, relation.sole
end
end

def test_first
assert_equal topics(:second).title, Topic.where("title = 'The Second Topic of the day'").first.title
end
Expand Down

0 comments on commit 5e361bb

Please sign in to comment.