1

Fix `eager_loading?` when ordering with `Hash` syntax by intrip · Pull Request #...

 2 years ago
source link: https://github.com/rails/rails/pull/42782
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Conversation

Copy link

Contributor

intrip commented 5 days ago

edited

Summary

eager_loading? is triggered correctly when using order with hash syntax on an outer table.

before:

Post.includes(:comments).order({ "comments.label": :ASC }).eager_loading?
=> raises ActiveRecord::StatementInvalid

after:

Post.includes(:comments).order({ "comments.label": :ASC }).eager_loading?
=> true

intrip

force-pushed the

intrip:fix-eager-loading-with-order-hash

branch from 5a51121 to 4756c51

5 days ago

intrip

marked this pull request as ready for review

5 days ago

intrip

force-pushed the

intrip:fix-eager-loading-with-order-hash

branch 2 times, most recently from e9b3b59 to 49fb99a

5 days ago

Copy link

Contributor

Author

intrip commented 5 days ago

@ghiculescu can you please take a look at this fix? bow

Copy link

Member

ghiculescu commented 5 days ago

+1 it looks good to me.

references = order_args.map do |arg|

case arg

when String

arg

when Hash

arg.keys

end

end.flatten

Comment on lines

1551 to 1558

eugeneius 5 days ago

Member

Suggested change
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.

intrip 4 days ago

edited

Author

Contributor

@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.

eugeneius 4 days ago

Member

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.

intrip

force-pushed the

intrip:fix-eager-loading-with-order-hash

branch from a060295 to 59f0609

4 days ago

intrip

force-pushed the

intrip:fix-eager-loading-with-order-hash

branch from 59f0609 to 8ab892e

4 days ago

eugeneius

merged commit 110d365 into rails:main 4 days ago

4 checks passed

Copy link

Member

eugeneius commented 4 days ago

Thanks @intrip!

intrip

deleted the

intrip:fix-eager-loading-with-order-hash

branch

4 days ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK