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

ActiveRecord::Relation#last ignores limit #371

Closed
dmarkow opened this issue May 3, 2011 · 3 comments
Closed

ActiveRecord::Relation#last ignores limit #371

dmarkow opened this issue May 3, 2011 · 3 comments

Comments

@dmarkow
Copy link
Contributor

dmarkow commented May 3, 2011

The limit option is ignored when calling #last on an ActiveRecord::Relation object:

Item.where("id <= 3").order(:id).limit(2)
# returns records #1 and #2
Item.where("id <= 3").order(:id).limit(2).last
# returns record #3, not record #2 as expected, since the limit is overridden by last
Item.where("id <= 3").order(:id).limit(2).all.last
# executing the query uses Array#last instead of ActiveRecord::Relation#last, and returns #2 as expected
@baroquebobcat
Copy link
Contributor

What would you expect it to return if there were less than the limit number of objects?

The reason limit is ignored is because find_last resets it to limit(1) after reversing the order. Have you tried using offset to get the behavior you want?

baroquebobcat added a commit to baroquebobcat/rails that referenced this issue May 7, 2011
if a query contains a limit or an offset, ActiveRecord::FinderMethods#find_last had inconsistent behavior.

If the records were loaded, it returned the last record in the cached list.
If they were not, it reversed the order of the query and changed the limit to one.
If the earlier limit was less than the total matching the query in the db,
it would return a different record than if the records had been cached.

This commit changes find_last so that it loads the records when getting the
last record on a query containing a limit or an offset, which makes the behavior consistent.
@baroquebobcat
Copy link
Contributor

I put together a patch #451

@josevalim
Copy link
Contributor

Closing this in favor of the pull request.

jonleighton pushed a commit that referenced this issue May 8, 2011
if a query contains a limit or an offset, ActiveRecord::FinderMethods#find_last had inconsistent behavior.

If the records were loaded, it returned the last record in the cached list.
If they were not, it reversed the order of the query and changed the limit to one.
If the earlier limit was less than the total matching the query in the db,
it would return a different record than if the records had been cached.

This commit changes find_last so that it loads the records when getting the
last record on a query containing a limit or an offset, which makes the behavior consistent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants