diff options
author | Joshua Wehner <joshua.wehner@gmail.com> | 2011-08-31 15:04:41 -0400 |
---|---|---|
committer | Joshua Wehner <joshua.wehner@gmail.com> | 2011-08-31 16:17:28 -0400 |
commit | 0df27c98d982ec87d2fb48cfda82694eb267993e (patch) | |
tree | 79546193dee01f52f9672fee59e50323a13f10b6 | |
parent | 508f33f35c9ce7a8973c1cf3466223313dfcfb24 (diff) | |
download | rails-0df27c98d982ec87d2fb48cfda82694eb267993e.tar.gz rails-0df27c98d982ec87d2fb48cfda82694eb267993e.tar.bz2 rails-0df27c98d982ec87d2fb48cfda82694eb267993e.zip |
Fixes bug in ActiveRecord::QueryMethods, #1697
Replace split on comma with a regexp that will reverse all ASC/DESC specifically
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 5 |
2 files changed, 7 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 7eda9ad8e8..355540782f 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -305,10 +305,8 @@ module ActiveRecord when Arel::Nodes::Ordering o.reverse when String, Symbol - o.to_s.split(',').collect do |s| - s.strip! - s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC') - end + s = o.to_s.gsub(/\s((desc)|(asc))\s*(,|\Z)/i) { |m| " #{$2 ? 'ASC' : 'DESC'}#{$4}" } + s.match(/\s(de|a)sc\Z/i) ? s : s.concat(" DESC") else o end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index da96afd718..c3bad58174 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -934,6 +934,11 @@ class RelationTest < ActiveRecord::TestCase assert_equal 'zyke', FastCar.order_using_old_style.limit(1).first.name end + def test_order_with_function_and_last + authors = Author.scoped + assert_equal authors(:bob), authors.order( "id asc, COALESCE( organization_id, owned_essay_id)" ).last + end + def test_order_using_scoping car1 = CoolCar.order('id DESC').scoping do CoolCar.find(:first, :order => 'id asc') |