Repository navigation
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. |
There was a problem hiding this comment.
| 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.
@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.
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 usingorderwith hash syntax on an outer table.before:
after: