From db930ec0fddc2e5e95029b5d761e1207a10b1488 Mon Sep 17 00:00:00 2001 From: Finn Young Date: Sun, 17 Feb 2019 00:41:30 +0000 Subject: [PATCH] Raise ActiveRecord::IrreversibleOrderError if nulls first/last is not a single ordering argument. --- activerecord/lib/active_record/relation/query_methods.rb | 2 +- activerecord/test/cases/relations_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 75976aa8fc3..ad8c3aba61c 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 857d7436053..51acbe7728b 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -290,9 +290,18 @@ class RelationTest < ActiveRecord::TestCase 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 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