aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorFinn Young <finnyoung88@gmail.com>2019-02-17 00:41:30 +0000
committerFinn Young <finnyoung88@gmail.com>2019-02-17 00:41:30 +0000
commitdb930ec0fddc2e5e95029b5d761e1207a10b1488 (patch)
tree1758bfa213e01880f3b1344d3587b61d991d2bc7 /activerecord
parentac68550ae6d117b1d257f5df38ba76e03b7e2cf4 (diff)
downloadrails-db930ec0fddc2e5e95029b5d761e1207a10b1488.tar.gz
rails-db930ec0fddc2e5e95029b5d761e1207a10b1488.tar.bz2
rails-db930ec0fddc2e5e95029b5d761e1207a10b1488.zip
Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a single ordering argument.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb9
2 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 75976aa8fc..ad8c3aba61 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -1111,7 +1111,7 @@ module ActiveRecord
# Uses SQL function with multiple arguments.
(order.include?(",") && order.split(",").find { |section| section.count("(") != section.count(")") }) ||
# Uses "nulls first" like construction.
- /nulls (first|last)\Z/i.match?(order)
+ /\bnulls\s+(?:first|last)\b/i.match?(order)
end
def build_order(arel)
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 857d743605..51acbe7728 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -291,8 +291,17 @@ class RelationTest < ActiveRecord::TestCase
Topic.order(Arel.sql("title NULLS FIRST")).reverse_order
end
assert_raises(ActiveRecord::IrreversibleOrderError) do
+ Topic.order(Arel.sql("title NULLS FIRST")).reverse_order
+ end
+ assert_raises(ActiveRecord::IrreversibleOrderError) do
Topic.order(Arel.sql("title nulls last")).reverse_order
end
+ assert_raises(ActiveRecord::IrreversibleOrderError) do
+ Topic.order(Arel.sql("title NULLS FIRST, author_name")).reverse_order
+ end
+ assert_raises(ActiveRecord::IrreversibleOrderError) do
+ Topic.order(Arel.sql("author_name, title nulls last")).reverse_order
+ end
end
def test_default_reverse_order_on_table_without_primary_key