Skip to content

Commit

Permalink
Merge pull request rails#1849 from raviolicode/postgresql-fix-3-1
Browse files Browse the repository at this point in the history
[3-1-stable] Fix for complex ordering of multiple columns on postgresql
  • Loading branch information
spastorino committed Jun 24, 2011
2 parents e65f114 + 975ff5d commit 54531b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }

Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 54531b8

Please sign in to comment.