Skip to content
Browse files

Merge pull request #1849 from raviolicode/postgresql-fix-3-1

[3-1-stable] Fix for complex ordering of multiple columns on postgresql
  • Loading branch information...
2 parents e65f114 + 975ff5d commit 54531b87b623f57b5949479fc6e759e16a411adf @spastorino spastorino committed Jun 24, 2011
View
2 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -929,7 +929,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
@@ -164,6 +164,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

0 comments on commit 54531b8

Please sign in to comment.
Something went wrong with that request. Please try again.