File tree Expand file tree Collapse file tree 2 files changed +23
-9
lines changed
lib/active_record/relation Expand file tree Collapse file tree 2 files changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -2081,17 +2081,29 @@ def column_references(order_args)
2081
2081
order_args . flat_map do |arg |
2082
2082
case arg
2083
2083
when String , Symbol
2084
- arg
2084
+ extract_table_name_from ( arg )
2085
2085
when Hash
2086
- arg . keys . select { |e | e . is_a? ( String ) || e . is_a? ( Symbol ) }
2086
+ arg
2087
+ . map do |key , value |
2088
+ case value
2089
+ when Hash
2090
+ key . to_s
2091
+ else
2092
+ extract_table_name_from ( key ) if key . is_a? ( String ) || key . is_a? ( Symbol )
2093
+ end
2094
+ end
2095
+ when Arel ::Attribute
2096
+ arg . relation . name
2097
+ when Arel ::Nodes ::Ordering
2098
+ if arg . expr . is_a? ( Arel ::Attribute )
2099
+ arg . expr . relation . name
2100
+ end
2087
2101
end
2088
- end . filter_map do |arg |
2089
- arg =~ /^\W ?(\w +)\W ?\. / && $1
2090
- end +
2091
- order_args
2092
- . select { |e | e . is_a? ( Hash ) }
2093
- . flat_map { |e | e . map { |k , v | k if v . is_a? ( Hash ) } }
2094
- . compact
2102
+ end . compact
2103
+ end
2104
+
2105
+ def extract_table_name_from ( string )
2106
+ string . match ( /^\W ?(\w +)\W ?\. / ) && $1
2095
2107
end
2096
2108
2097
2109
def order_column ( field )
Original file line number Diff line number Diff line change @@ -55,11 +55,13 @@ def test_order_with_association
55
55
assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( "authors.name" , books : { name : :asc } ) )
56
56
assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( "authors.name" , "books.name" ) )
57
57
assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( { authors : { name : :asc } } , Book . arel_table [ :name ] ) )
58
+ assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( Author . arel_table [ :name ] , Book . arel_table [ :name ] ) )
58
59
59
60
author_desc_then_book_name = [ y , x , z ]
60
61
61
62
assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( authors : { name : :desc } , books : { name : :asc } ) )
62
63
assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( "authors.name desc" , books : { name : :asc } ) )
64
+ assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( Author . arel_table [ :name ] . desc , books : { name : :asc } ) )
63
65
assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( { authors : { name : :desc } } , :name ) )
64
66
end
65
67
end
You can’t perform that action at this time.
0 commit comments