Skip to content
Browse files

Merge pull request #2775 from jaw6/fix/reverse

Fixes bug in ActiveRecord::QueryMethods, #1697
  • Loading branch information...
2 parents 7a7a3bc + 0df27c9 commit 90248d2953789f8631480c767ebf3accdbda6a7a @tenderlove tenderlove committed
View
6 activerecord/lib/active_record/relation/query_methods.rb
@@ -305,10 +305,8 @@ def reverse_sql_order(order_query)
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
View
5 activerecord/test/cases/relations_test.rb
@@ -934,6 +934,11 @@ def test_default_scope_order_with_scope_order
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')

0 comments on commit 90248d2

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