1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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 <elkenny@gmail.com>
This commit is contained in:
Jacopo 2021-07-14 11:08:58 +02:00
parent a3f9568cb6
commit 8ab892e7b1
3 changed files with 40 additions and 1 deletions

View file

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

View file

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

View file

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