diff options
author | Lucia Escanellas <raviolicode@gmail.com> | 2011-06-23 22:32:22 -0300 |
---|---|---|
committer | Lucia Escanellas <raviolicode@gmail.com> | 2011-06-24 13:37:08 -0300 |
commit | 9734a416faaa8149fa5914b0afe2e6761ad5ec20 (patch) | |
tree | 1f4873033932715031f163b42b5ebd49b347d23d /activerecord | |
parent | 8a1319dec09672cbb1a4d6fce1db09f10c44bceb (diff) | |
download | rails-9734a416faaa8149fa5914b0afe2e6761ad5ec20.tar.gz rails-9734a416faaa8149fa5914b0afe2e6761ad5ec20.tar.bz2 rails-9734a416faaa8149fa5914b0afe2e6761ad5ec20.zip |
Improve ordering of multiple columns on postgresql
* Only on postgresql, order("first asc, second asc") was invalid
* Closes #1720
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 7 |
2 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 6d638628df..29dd0a9ea3 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -940,7 +940,7 @@ module ActiveRecord # 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}" } diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index aa94c9b87e..d88bde3860 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -176,6 +176,13 @@ class RelationTest < ActiveRecord::TestCase 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 |