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 QueryMethods#in_order_of to handle empty order list #43916

Merged

Conversation

casperisfine
Copy link
Contributor

@casperisfine casperisfine commented Dec 17, 2021

Fix: #43903
Ref: #42061

Simply don't generate the case statement if the order list is empty
and fallback to default ordering.

cc @kddnewton @kaspth

TODO:

  • backport to 7.0

Fix: rails#43903
Ref: rails#42061

Simply don't generate the case statement if the order list is empty
and fallback to default ordering.
@rafaelfranca rafaelfranca merged commit 68075af into rails:main Dec 20, 2021
@rafaelfranca rafaelfranca deleted the activerecord-in-order-of-empty-array branch December 20, 2021 22:42
rafaelfranca added a commit that referenced this pull request Dec 20, 2021
…-array

Fix `QueryMethods#in_order_of` to handle empty order list
@kddnewton
Copy link
Contributor

Thanks for the fix @casperisfine, so I missed it in the first place!

@jonathanhefner
Copy link
Member

Something I noticed from this test is that Active Record's in_order_of behaves differently than Enumerable#in_order_of:

posts = Post.all
posts.in_order_of(:id, order).pluck(:id)      # => [3, 4, 1, 2, 5, 6, 7, 8, 9, 10, 11]
posts.to_a.in_order_of(:id, order).pluck(:id) # => [3, 4, 1]

I feel like the two methods should be consistent, but I am unsure which is the preferred behavior. Thoughts?

@rafaelfranca
Copy link
Member

I think it should match the enumerable behavior.

@kddnewton
Copy link
Contributor

I've added the PR here: #44097. This brings it in line with the enumerable version.

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.

ActiveRecord::Relation.in_order_of produces invalid SQL with empty array
5 participants