aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Toews <mastahyeti@gmail.com>2017-10-12 11:48:48 -0600
committerMatthew Draper <matthew@trebex.net>2017-11-09 22:42:15 +1030
commitb76cc29865fb69389ffdb7bd9f8085aa86354f82 (patch)
treef546036743fc345ad94aebffab24a5b51821f074
parentc711a27d29a3201ff47751a1d788f1e634186dd3 (diff)
downloadrails-b76cc29865fb69389ffdb7bd9f8085aa86354f82.tar.gz
rails-b76cc29865fb69389ffdb7bd9f8085aa86354f82.tar.bz2
rails-b76cc29865fb69389ffdb7bd9f8085aa86354f82.zip
deal with Array arguments to #order
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb14
-rw-r--r--activerecord/lib/active_record/sanitization.rb6
-rw-r--r--activerecord/test/cases/relations_test.rb6
3 files changed, 22 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index db7fe8123d..1dcd786498 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -930,7 +930,19 @@ module ActiveRecord
# Extract column names from arguments passed to #order or #reorder.
def column_names_from_order_arguments(args)
- args.flat_map { |arg| arg.is_a?(Hash) ? arg.keys : arg }
+ args.flat_map do |arg|
+ case arg
+ when Hash
+ # Tag.order(id: :desc)
+ arg.keys
+ when Array
+ # Tag.order([Arel.sql("field(id, ?)"), [1, 3, 2]])
+ arg.flatten
+ else
+ # Tag.order(:id)
+ arg
+ end
+ end
end
def assert_mutability!
diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb
index 1c3099f55c..4caf1145f0 100644
--- a/activerecord/lib/active_record/sanitization.rb
+++ b/activerecord/lib/active_record/sanitization.rb
@@ -63,6 +63,12 @@ module ActiveRecord
# # => "id ASC"
def sanitize_sql_for_order(condition) # :doc:
if condition.is_a?(Array) && condition.first.to_s.include?("?")
+ # Ensure we aren't dealing with a subclass of String that might
+ # override methods we use (eg. Arel::Nodes::SqlLiteral).
+ if condition.first.kind_of?(String) && !condition.first.instance_of?(String)
+ condition = [String.new(condition.first), *condition[1..-1]]
+ end
+
sanitize_sql_array(condition)
else
condition
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 4c865ef965..a755a3ceeb 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -389,13 +389,13 @@ class RelationTest < ActiveRecord::TestCase
end
def test_finding_with_sanitized_order
- query = Tag.order(["field(id, ?)", [1, 3, 2]]).to_sql
+ query = Tag.order([Arel.sql("field(id, ?)"), [1, 3, 2]]).to_sql
assert_match(/field\(id, 1,3,2\)/, query)
- query = Tag.order(["field(id, ?)", []]).to_sql
+ query = Tag.order([Arel.sql("field(id, ?)"), []]).to_sql
assert_match(/field\(id, NULL\)/, query)
- query = Tag.order(["field(id, ?)", nil]).to_sql
+ query = Tag.order([Arel.sql("field(id, ?)"), nil]).to_sql
assert_match(/field\(id, NULL\)/, query)
end