Skip to content

Commit bfc45da

Browse files
authored
Merge pull request rails#53514 from kamipo/fix_order_with_using_association_alias
Fix `order` with using association name as an alias
2 parents 3e83be7 + 243dfe3 commit bfc45da

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ def column_references(order_args)
21312131
arg.expr.relation.name
21322132
end
21332133
end
2134-
end.compact
2134+
end.filter_map { |ref| Arel.sql(ref, retryable: true) if ref }
21352135
end
21362136

21372137
def extract_table_name_from(string)

activerecord/test/cases/relation/order_test.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
class OrderTest < ActiveRecord::TestCase
88
fixtures :authors, :author_addresses
99

10+
def setup
11+
Book.delete_all
12+
end
13+
1014
def test_order_asc
11-
Book.destroy_all
1215
z = Book.create!(name: "Zulu", author: authors(:david))
1316
y = Book.create!(name: "Yankee", author: authors(:mary))
1417
x = Book.create!(name: "X-Ray", author: authors(:david))
@@ -26,7 +29,6 @@ def test_order_asc
2629
end
2730

2831
def test_order_desc
29-
Book.destroy_all
3032
z = Book.create!(name: "Zulu", author: authors(:david))
3133
y = Book.create!(name: "Yankee", author: authors(:mary))
3234
x = Book.create!(name: "X-Ray", author: authors(:david))
@@ -44,7 +46,6 @@ def test_order_desc
4446
end
4547

4648
def test_order_with_association
47-
Book.destroy_all
4849
z = Book.create!(name: "Zulu", author: authors(:david))
4950
y = Book.create!(name: "Yankee", author: authors(:mary))
5051
x = Book.create!(name: "X-Ray", author: authors(:david))
@@ -64,4 +65,26 @@ def test_order_with_association
6465
assert_equal(author_desc_then_book_name, Book.includes(:author).order(Author.arel_table[:name].desc, books: { name: :asc }))
6566
assert_equal(author_desc_then_book_name, Book.includes(:author).order({ authors: { name: :desc } }, :name))
6667
end
68+
69+
def test_order_with_association_alias
70+
z = Book.create!(name: "Zulu", author: authors(:david))
71+
y = Book.create!(name: "Yankee", author: authors(:mary))
72+
x = Book.create!(name: "X-Ray", author: authors(:david))
73+
74+
author_name = Author.arel_table.alias("author")[:name]
75+
76+
author_then_book_name = [x, z, y]
77+
78+
assert_equal(author_then_book_name, Book.includes(:author).order(author: { name: :asc }, books: { name: :asc }))
79+
assert_equal(author_then_book_name, Book.includes(:author).order("author.name", books: { name: :asc }))
80+
assert_equal(author_then_book_name, Book.includes(:author).order({ author: { name: :asc } }, :name))
81+
assert_equal(author_then_book_name, Book.includes(:author).order(author_name, :name))
82+
83+
author_desc_then_book_name = [y, x, z]
84+
85+
assert_equal(author_desc_then_book_name, Book.includes(:author).order(author: { name: :desc }, books: { name: :asc }))
86+
assert_equal(author_desc_then_book_name, Book.includes(:author).order("author.name desc", books: { name: :asc }))
87+
assert_equal(author_desc_then_book_name, Book.includes(:author).order({ author: { name: :desc } }, :name))
88+
assert_equal(author_desc_then_book_name, Book.includes(:author).order(author_name.desc, :name))
89+
end
6790
end

0 commit comments

Comments
 (0)