Skip to content

Commit d4acb7a

Browse files
authored
Merge pull request rails#54307 from matthewd/sole-sole
Use already-loaded relation contents in #sole
2 parents b16397c + 4846c70 commit d4acb7a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

activerecord/lib/active_record/relation/finder_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def take!
141141
#
142142
# Product.where(["price = %?", price]).sole
143143
def sole
144-
found, undesired = limit(2)
144+
found, undesired = take(2)
145145

146146
if found.nil?
147147
raise_record_not_found_exception!

activerecord/test/cases/finder_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,15 @@ def test_sole_failing_many
794794
end
795795
end
796796

797+
def test_sole_on_loaded_relation
798+
relation = Topic.where("title = 'The First Topic'").load
799+
expected_topic = topics(:first)
800+
801+
assert_no_queries do
802+
assert_equal expected_topic, relation.sole
803+
end
804+
end
805+
797806
def test_first
798807
assert_equal topics(:second).title, Topic.where("title = 'The Second Topic of the day'").first.title
799808
end

0 commit comments

Comments
 (0)