From 8ab892e7b1fa9c4dfb4180aeb5e202b3adcba137 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Wed, 14 Jul 2021 11:08:58 +0200 Subject: [PATCH] Fix `eager_loading?` when ordering with `Hash` syntax `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 --- activerecord/CHANGELOG.md | 12 +++++++++++ .../active_record/relation/query_methods.rb | 9 ++++++++- activerecord/test/cases/relations_test.rb | 20 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 15c626f004..8b0e4a2f1e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,15 @@ +* Fix `eager_loading?` when ordering with `Hash` syntax + + `eager_loading?` is triggered correctly when using `order` with hash syntax + on an outer table. + + ```ruby + Post.includes(:comments).order({ "comments.label": :ASC }).eager_loading? + => true + ``` + + *Jacopo Beschi* + * Move the forcing of clear text encoding to the `ActiveRecord::Encryption::Encryptor`. Fixes #42699. diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index bdd36db298..7bfed81f3e 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1548,7 +1548,14 @@ module ActiveRecord end def column_references(order_args) - references = order_args.grep(String) + references = order_args.flat_map do |arg| + case arg + when String + arg + when Hash + arg.keys + end + end references.map! { |arg| arg =~ /^\W?(\w+)\W?\./ && $1 }.compact! references end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 42951a4856..d630c97299 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1704,6 +1704,26 @@ class RelationTest < ActiveRecord::TestCase assert_not_predicate scope, :eager_loading? end + def test_order_triggers_eager_loading + scope = Post.includes(:comments).order("comments.label ASC") + assert_predicate scope, :eager_loading? + end + + def test_order_doesnt_trigger_eager_loading_when_ordering_using_the_owner_table + scope = Post.includes(:comments).order("posts.title ASC") + assert_not_predicate scope, :eager_loading? + end + + def test_order_triggers_eager_loading_when_ordering_using_hash_syntax + scope = Post.includes(:comments).order({ "comments.label": :ASC }) + assert_predicate scope, :eager_loading? + end + + def test_order_doesnt_trigger_eager_loading_when_ordering_using_the_owner_table_and_hash_syntax + scope = Post.includes(:comments).order({ "posts.title": :ASC }) + assert_not_predicate scope, :eager_loading? + end + def test_automatically_added_where_references scope = Post.where(comments: { body: "Bla" }) assert_equal ["comments"], scope.references_values