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
Add prepend_order
as a new query method to prepend the specified order onto any existing order
#40401
base: main
Are you sure you want to change the base?
Conversation
Hi @agrobbin. Thanks for the PR.
|
@p8 I looked at User.order('email DESC').reorder('id ASC') # generated SQL has `ORDER BY id ASC`
User.order('email DESC').preorder('id ASC') # generated SQL has `ORDER BY id ASC, email DESC` |
@agrobbin Ah that makes sense. |
🤔 my initial idea regarding this was to provide block to |
38c3a5b
to
0b0b8b5
Compare
@simi I'm definitely open to other approaches here, this just felt like the simplest one with the least likelihood of impacting existing applications! I bet we could achieve the same result by changing |
0b0b8b5
to
5b570a2
Compare
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. |
I'd love to get some input on this contribution if anyone from the core team has some time! |
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. |
I think in this case you'd want to do User.order('email DESC').reorder('ORDER BY id ASC, email DESC') I don't mind this new addition. My main concern is the name. To me |
@ghiculescu I'm totally open to a different name! I do like the simplicity of |
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. |
I'd still love to get some 👀 on this for potential inclusion! |
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. |
I'd still love to get this into Rails core! |
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. |
I'd still love to get this into Rails core! |
@agrobbin Maybe prepend_order would be a clearer name? Similar to reverse_order… |
5b570a2
to
f89077b
Compare
@p8 updated! I've also rebased off of the latest |
preorder
as a new query method to prepend the specified order onto any existing orderprepend_order
as a new query method to prepend the specified order onto any existing order
I hadn't seen those @p8! I'm hopeful there is openness to this feature now, particularly since based on what was said back in #16051, the opposition wasn't to the feature itself but to the changing of the default over time. Since this is purely additive, I've got my 🤞🏻 that this is acceptable! If |
…der onto any existing order ```ruby class User < ApplicationRecord scope :recent, -> { order(name: :desc) } end most_popular_then_recent = User.recent.prepend_order(popularity: :desc) ``` This is useful if you are using a scope that has an order that you want use as a fallback.
f89077b
to
98b90e7
Compare
@p8 I know it's been awhile (actually, almost exactly a year!), but I'm wondering if you have a few moments to take another look at this. |
Summary
I recently ran into a situation where it would've been great to prepend a new
ORDER BY
clause to a query. In our case, it was when using ascope
that itself had something to the effect of:We wanted to use
User.recently_updated
, but needed to put a specific group of users at the top of the list. The only way to do that without something likeprepend_order
would be to reimplement therecently_updated
scopes defined on the model.With
prepend_order
, we can simply callUser.recently_updated.prepend_order('...')
, and we're done!Another way we could use this would be in conjunction with
ActiveRecord::FinderMethods#ordered_relation
: