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 AR::Relation#last bugs instroduced in 7705fc #23377

Merged
merged 1 commit into from Feb 13, 2016

Conversation

bogdan
Copy link
Contributor

@bogdan bogdan commented Jan 31, 2016

Fix bugs identifier in #16400 (comment)

I realized that last should work pretty different on a relation with limit and without it.
So the fix is pretty different from what we all expected:
We need to use offset instead of reverse_order on such a relation.

cc @eileencodes @rafaelfranca @sgrif

@rails-bot
Copy link

r? @eileencodes

(@rails-bot has picked a reviewer for you, use r? to override)


result = limit(sql_limit)
if initial_limit
result.offset!((offset_value || 0) + initial_limit - sql_limit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just stick with to_a here? This assumes there are at least offset_value + initial_limit rows in the original relation, doesn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this patch implementation people have a choice between relation.last and relation.to_a.last. In your variant, there will be no such choice. I vote for this patch version, but open for an argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my argument is that before we started changing it, it always returned the right answer. Now it doesn't, and people may be forced to call to_a to make it work. Performance is nice and all, but that doesn't seem like a win. 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with behaviour comes from lack of tests.

I tried some combinations of offset and limit on a relation where last is called and didn't find a different behaviour from previous version. Can you guess what else it could be?

It would be cool to add a test anyway even we go with to_a.last when limit present on basic relation at the end anyway.

@eileencodes
Copy link
Member

This fixes the issue I'm seeing in our app 👍

def find_last(limit)
limit ? to_a.last(limit) : to_a.last
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, can you remove this extra line.

@bogdan
Copy link
Contributor Author

bogdan commented Feb 1, 2016

@eileencodes removed whitespace. Anything else?

@eileencodes eileencodes added this to the 5.0.0 milestone Feb 1, 2016
sgrif added a commit that referenced this pull request Feb 1, 2016
This reverts commit 9f3730a, reversing
changes made to 2637fb7.

There are additional issues with this commit that need to be addressed
before this change is ready (see #23377). This is a temporary revert in
order for us to have more time to address the issues with that PR,
without blocking the release of beta2.
@bogdan
Copy link
Contributor Author

bogdan commented Feb 2, 2016

Updated PR. Current version contains:

@dmathieu if you have a test that we can add to a test suite, please suggest. I still didn't a use case that is not covered.

@dhh
Copy link
Member

dhh commented Feb 10, 2016

@eileencodes can you verify if this is good to go back in?

@eileencodes
Copy link
Member

Hey @bogdan there's a conflict in the last method when I rebase. Can you take a look at it so I can test this against basecamp? Thanks!

@eileencodes
Copy link
Member

I tested and this looks good. If you can rebase we can merge this again.

@bogdan bogdan force-pushed the last-with-sql branch 6 times, most recently from 9f0d765 to cedc362 Compare February 13, 2016 08:32
instead of loading the relation into memory
@bogdan
Copy link
Contributor Author

bogdan commented Feb 13, 2016

@eileencodes done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants