Browse files

Fix an edge case with find with a list of ids, limit, and offset. Clo…

…ses #8437.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6912 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 0cf79f0 commit 8158455ded5811271e7d8530f262bc74a1677334 @jeremy jeremy committed May 31, 2007
Showing with 13 additions and 4 deletions.
  1. +6 −4 activerecord/lib/active_record/base.rb
  2. +7 −0 activerecord/test/finder_test.rb
View
10 activerecord/lib/active_record/base.rb
@@ -1032,16 +1032,18 @@ def find_some(ids, options)
result = find_every(options)
- # If the user passes in a limit to find(), we need to check
- # to see if the result is limited before just checking the
- # size of the results.
+ # Determine expected size from limit and offset, not just ids.size.
expected_size =
if options[:limit] && ids.size > options[:limit]
options[:limit]
else
ids.size
end
- expected_size -= options[:offset] if options[:offset]
+
+ # 11 ids with limit 3, offset 9 should give 2 results.
+ if options[:offset] && (ids.size - options[:offset] < expected_size)
+ expected_size = ids.size - options[:offset]
+ end
if result.size == expected_size
result
View
7 activerecord/test/finder_test.rb
@@ -44,6 +44,13 @@ def test_find_by_ids
def test_find_by_ids_with_limit_and_offset
assert_equal 2, Entrant.find([1,3,2], :limit => 2).size
assert_equal 1, Entrant.find([1,3,2], :limit => 3, :offset => 2).size
+
+ # Also test an edge case: If you have 11 results, and you set a
+ # limit of 3 and offset of 9, then you should find that there
+ # will be only 2 results, regardless of the limit.
+ devs = Developer.find :all
+ last_devs = Developer.find devs.map(&:id), :limit => 3, :offset => 9
+ assert_equal 2, last_devs.size
end
def test_find_an_empty_array

0 comments on commit 8158455

Please sign in to comment.