![](/style/images/good.png)
![](/style/images/bad.png)
Fix `eager_loading?` when ordering with `Hash` syntax by intrip · Pull Request #...
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
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
force-pushed the
intrip:fix-eager-loading-with-order-hash
@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
Comment on lines
1551 to 1558
eugeneius 5 days ago
Member
If any of the hash's keys are arrays, we don't want to flatten them.
intrip 4 days ago •
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.
Thanks @intrip!
No one assigned
None yet
No milestone
Successfully merging this pull request may close these issues.
None yet
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK