Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1848 from raviolicode/postgresql_order_multiple_p…

…arams

Fix for complex ordering of multiple columns on postgresql
  • Loading branch information...
commit d6413f043ce132618c7b3d50301d66eac339f997 2 parents 7ca1a3d + 9734a41
@spastorino spastorino authored
View
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -940,7 +940,7 @@ def distinct(columns, orders) #:nodoc:
# Construct a clean list of column names from the ORDER BY clause, removing
# any ASC/DESC modifiers
- order_columns = orders.collect { |s| s =~ /^(.+)\s+(ASC|DESC)\s*$/i ? $1 : s }
+ order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*/i, '') }
order_columns.delete_if { |c| c.blank? }
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
View
7 activerecord/test/cases/relations_test.rb
@@ -176,6 +176,13 @@ def test_finding_with_order_and_take
assert_equal entrants(:first).name, entrants.first.name
end
+ def test_finding_with_cross_table_order_and_limit
+ tags = Tag.includes(:taggings) \
+ .order("tags.name asc, taggings.taggable_id asc, REPLACE('abc', taggings.taggable_type, taggings.taggable_type)") \
+ .limit(1).to_a
+ assert_equal 1, tags.length
+ end
+
def test_finding_with_complex_order_and_limit
tags = Tag.includes(:taggings).order("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)").limit(1).to_a
assert_equal 1, tags.length
Please sign in to comment.
Something went wrong with that request. Please try again.