Skip to content

Commit f8362b6

Browse files
authored
Merge pull request rails#43744 from martinjaimem/enhance/many-one
Enhance many? one?
2 parents be3ed75 + 02d9f31 commit f8362b6

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

activerecord/lib/active_record/relation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,14 @@ def any?
290290
# Returns true if there is exactly one record.
291291
def one?
292292
return super if block_given?
293-
return records.one? if limit_value || loaded?
293+
return records.one? if loaded?
294294
limited_count == 1
295295
end
296296

297297
# Returns true if there is more than one record.
298298
def many?
299299
return super if block_given?
300-
return records.many? if limit_value || loaded?
300+
return records.many? if loaded?
301301
limited_count > 1
302302
end
303303

@@ -974,7 +974,7 @@ def tables_in_string(string)
974974
end
975975

976976
def limited_count
977-
@limited_count ||= limit(2).count
977+
@limited_count ||= limit_value ? count : limit(2).count
978978
end
979979
end
980980
end

activerecord/test/cases/relations_test.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,14 @@ def test_many
11741174
end
11751175

11761176
def test_many_with_limits
1177-
posts = Post.all
1177+
posts_with_limit = Post.limit(5)
1178+
posts_with_limit_one = Post.limit(1)
11781179

1179-
assert_predicate posts, :many?
1180-
assert_not_predicate posts.limit(1), :many?
1180+
assert_predicate posts_with_limit, :many?
1181+
assert_not_predicate posts_with_limit, :loaded?
1182+
1183+
assert_not_predicate posts_with_limit_one, :many?
1184+
assert_not_predicate posts_with_limit_one, :loaded?
11811185
end
11821186

11831187
def test_none?

0 commit comments

Comments
 (0)