-
Notifications
You must be signed in to change notification settings - Fork 21.8k
Fix eager_loading?
when ordering with Hash
syntax
#42782
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 eager_loading?
when ordering with Hash
syntax
#42782
Conversation
5a51121
to
4756c51
Compare
e9b3b59
to
49fb99a
Compare
@ghiculescu can you please take a look at this fix? 🙇 |
👍 it looks good to me. |
references = order_args.map do |arg| | ||
case arg | ||
when String | ||
arg | ||
when Hash | ||
arg.keys | ||
end | ||
end.flatten |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
references = order_args.map do |arg| | |
case arg | |
when String | |
arg | |
when Hash | |
arg.keys | |
end | |
end.flatten | |
references = order_args.flat_map do |arg| | |
case arg | |
when String | |
arg | |
when Hash | |
arg.keys | |
end | |
end |
If any of the hash's keys are arrays, we don't want to flatten them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eugeneius Thanks!
I've applied the suggestion but the above use case it's unclear to me: could you show me an example of using Array
as Hash
keys for order
? I've tried but I couldn't manage to reproduce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't concerned about breaking a valid use case, more just that flat_map
corresponds better to what we're trying to do here. Using flap_map
also saves an intermediate array allocation so it should be (marginally) faster, which is probably worthwhile since this code runs every time a relation with an order is built.
a060295
to
59f0609
Compare
`eager_loading?` is triggered correctly when using `order` with hash syntax on an outer table. before: ```ruby Post.includes(:comments).order({ "comments.label": :ASC }).eager_loading? => raises ActiveRecord::StatementInvalid ``` after: ```ruby Post.includes(:comments).order({ "comments.label": :ASC }).eager_loading? => true ``` Co-authored-by: Eugene Kenny <elkenny@gmail.com>
59f0609
to
8ab892e
Compare
Thanks @intrip! |
Summary
eager_loading?
is triggered correctly when usingorder
with hash syntax on an outer table.before:
after: