Skip to content

Commit d6413f0

Browse files
committed
Merge pull request rails#1848 from raviolicode/postgresql_order_multiple_params
Fix for complex ordering of multiple columns on postgresql
2 parents 7ca1a3d + 9734a41 commit d6413f0

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def distinct(columns, orders) #:nodoc:
940940

941941
# Construct a clean list of column names from the ORDER BY clause, removing
942942
# any ASC/DESC modifiers
943-
order_columns = orders.collect { |s| s =~ /^(.+)\s+(ASC|DESC)\s*$/i ? $1 : s }
943+
order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*/i, '') }
944944
order_columns.delete_if { |c| c.blank? }
945945
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
946946

activerecord/test/cases/relations_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ def test_finding_with_order_and_take
176176
assert_equal entrants(:first).name, entrants.first.name
177177
end
178178

179+
def test_finding_with_cross_table_order_and_limit
180+
tags = Tag.includes(:taggings) \
181+
.order("tags.name asc, taggings.taggable_id asc, REPLACE('abc', taggings.taggable_type, taggings.taggable_type)") \
182+
.limit(1).to_a
183+
assert_equal 1, tags.length
184+
end
185+
179186
def test_finding_with_complex_order_and_limit
180187
tags = Tag.includes(:taggings).order("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)").limit(1).to_a
181188
assert_equal 1, tags.length

0 commit comments

Comments
 (0)