Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find_nth with limit_value #25274

Merged
merged 1 commit into from
Feb 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def first!
def last(limit = nil)
return find_last(limit) if loaded? || limit_value

result = limit(limit || 1)
result = limit(limit)
result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
result = result.reverse_order!

Expand Down Expand Up @@ -536,8 +536,12 @@ def find_nth_with_limit(index, limit)
self
end

relation = relation.offset(offset_index + index) unless index.zero?
relation.limit(limit).to_a
if limit_value.nil? || index < limit_value
relation = relation.offset(offset_index + index) unless index.zero?
relation.limit(limit).to_a
else
[]
end
end
end

Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def test_second_to_last
assert_nil Topic.offset(5).second_to_last

#test with limit
# assert_nil Topic.limit(1).second # TODO: currently failing
assert_nil Topic.limit(1).second
assert_nil Topic.limit(1).second_to_last
end

Expand Down Expand Up @@ -526,9 +526,9 @@ def test_third_to_last
assert_nil Topic.offset(5).third_to_last

# test with limit
# assert_nil Topic.limit(1).third # TODO: currently failing
assert_nil Topic.limit(1).third
assert_nil Topic.limit(1).third_to_last
# assert_nil Topic.limit(2).third # TODO: currently failing
assert_nil Topic.limit(2).third
assert_nil Topic.limit(2).third_to_last
end

Expand Down