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 first and last when records are not loaded in a sorted relationship #37807

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GermanDZ
Copy link

@GermanDZ GermanDZ commented Nov 26, 2019

Summary

Fix last and first methods on a sorted relationship with elements with the same order criteria.

A test was added in 9863eae and is failing with current implementation, for all databases tests, i.e.:

image

The underlying problem is that first and last when the relationship is not loaded in the parent record uses an optimization based on the database to retrieve the items. The optimization applies correctly the order criteria and also a LIMIT clause to minimize the retrieved data to only the amount of records requested (1 by default, but can be used for .last(4)).

The solution proposed is to add the default ordering criteria (based on implicit_order_column and primary_key) to disambiguate the sorting on the database.

Related issues

This PR adds a second column for implicit_order_column => #37626? Am I duplicating the solution and it's already solved? @pawurb?

Probably solving this issue #36475?

No, after reproducing the case with the fix applies is still failing.

@GermanDZ GermanDZ force-pushed the last-and-first-in-active-record-sorted-collection-must-work-as-expected branch 2 times, most recently from b137583 to 9863eae Compare November 27, 2019 06:41
@GermanDZ GermanDZ requested a review from kamipo January 28, 2020 18:16
@GermanDZ GermanDZ changed the title [WIP] Fix first and last when records are not loaded in a sorted relationship Fix first and last when records are not loaded in a sorted relationship Apr 13, 2020
@p8
Copy link
Member

p8 commented Jun 12, 2020

Hi @GermanDZ, Thanks for creating this PR. There also is a newer related ticket: #39525

@jonathanhefner
Copy link
Member

I agree with @p8 that #39525 would be a better approach to solve this, though it wouldn't be an "automatic" solution.

One problem with this PR is that it doesn't address the order of first, nor any of the other ordinal finders (e.g. owner.pets.second).

A larger problem, though, is that it affects last unconditionally. For example, Part.order(:serial_number).last will now always use ORDER BY "parts"."serial_number" DESC, "parts"."id" DESC, which is both unexpected and a potential performance issue.

@rails-bot
Copy link

rails-bot bot commented Sep 10, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Sep 10, 2020
@GermanDZ
Copy link
Author

@p8 / @jonathanhefner I've just added my failing test case (just a cherry-pick of my commit) to the #39525 PR and is still failing.

We should consider mix both solutions in a single PR? Maybe we could wait until the other PR is merged and then adapt this PR to fix "my" problem on top of the previous (better) solution.

WDYT?

@rails-bot rails-bot bot removed the stale label Sep 11, 2020
@rails-bot
Copy link

rails-bot bot commented Dec 10, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 10, 2020
@GermanDZ
Copy link
Author

Waiting for merging of #39525 to rebase and rework this solution

@rails-bot rails-bot bot removed the stale label Dec 14, 2020
Base automatically changed from master to main January 14, 2021 17:01
@rails-bot
Copy link

rails-bot bot commented Apr 14, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Apr 14, 2021
@GermanDZ
Copy link
Author

Still waiting for #39525 to rebase and rework this solution

@rails-bot rails-bot bot removed the stale label Apr 15, 2021
@rails-bot
Copy link

rails-bot bot commented Jul 14, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Jul 14, 2021
@GermanDZ
Copy link
Author

Still waiting for #39525 to rebase and rework this solution

@rails-bot rails-bot bot removed the stale label Jul 19, 2021
@rails-bot
Copy link

rails-bot bot commented Oct 17, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Oct 17, 2021
@GermanDZ
Copy link
Author

Still waiting for #39525 to rebase and rework this solution

@rails-bot rails-bot bot removed the stale label Oct 18, 2021
@rails-bot
Copy link

rails-bot bot commented Jan 16, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Jan 16, 2022
@GermanDZ
Copy link
Author

Still waiting for #39525 to rebase and rework this solution

@rails-bot rails-bot bot removed the stale label Jan 17, 2022
@rails-bot
Copy link

rails-bot bot commented Apr 17, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Apr 17, 2022
@GermanDZ
Copy link
Author

Still waiting for #39525 to rebase and rework this solution

@rails-bot rails-bot bot removed the stale label Apr 18, 2022
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

3 participants