aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Toews <mastahyeti@gmail.com>2017-10-11 14:15:47 -0600
committerMatthew Draper <matthew@trebex.net>2017-11-09 22:42:15 +1030
commitc711a27d29a3201ff47751a1d788f1e634186dd3 (patch)
tree6a23b09f48ef0bc30bb2159f595eb7a4a2bc8561
parentab03eb9f576312c75e61caaf9705a8ac5175c769 (diff)
downloadrails-c711a27d29a3201ff47751a1d788f1e634186dd3.tar.gz
rails-c711a27d29a3201ff47751a1d788f1e634186dd3.tar.bz2
rails-c711a27d29a3201ff47751a1d788f1e634186dd3.zip
convert order arg to string before checking if we can reverse it
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb4
-rw-r--r--activerecord/test/cases/relations_test.rb4
2 files changed, 6 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 34723b0b4f..db7fe8123d 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -1097,6 +1097,10 @@ module ActiveRecord
end
def does_not_support_reverse?(order)
+ # Account for String subclasses like Arel::Nodes::SqlLiteral that
+ # override methods like #count.
+ order = String.new(order) unless order.instance_of?(String)
+
# Uses SQL function with multiple arguments.
(order.include?(",") && order.split(",").find { |section| section.count("(") != section.count(")") }) ||
# Uses "nulls first" like construction.
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index f3f82e7591..4c865ef965 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -248,9 +248,9 @@ class RelationTest < ActiveRecord::TestCase
end
def test_reverse_order_with_function_other_predicates
- topics = Topic.order("author_name, length(title), id").reverse_order
+ topics = Topic.order(Arel.sql("author_name, length(title), id")).reverse_order
assert_equal topics(:second).title, topics.first.title
- topics = Topic.order("length(author_name), id, length(title)").reverse_order
+ topics = Topic.order(Arel.sql("length(author_name), id, length(title)")).reverse_order
assert_equal topics(:fifth).title, topics.first.title
end