From 51f48f0ed9ec84fd12584b750609a2f7e945b945 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 23 Oct 2014 14:53:20 -0600 Subject: `#not_in` with a range should respect proper precedence Currently, doing ```ruby relation[:id].not_eq(4).and(relation[:id].not_in(1..3)) ``` will generate ```sql "id" != 4 AND "id" < 1 OR "id" > 3 ``` Which would incorrectly include records with an id of 4, as the OR statement has higher precidence than the AND statement. The `or` method on `Node` properly groups the statement in parenthesis. --- lib/arel/predications.rb | 2 +- test/visitors/test_to_sql.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb index 3050526a43..6f9a3c5f91 100644 --- a/lib/arel/predications.rb +++ b/lib/arel/predications.rb @@ -83,7 +83,7 @@ module Arel else right = Nodes::GreaterThan.new(self, Nodes.build_quoted(other.end, self)) end - Nodes::Or.new left, right + left.or(right) end when Array Nodes::NotIn.new self, other.map { |x| Nodes.build_quoted(x, self) } diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index abd8cfe356..62e1c57c73 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -480,16 +480,16 @@ module Arel it 'can handle two dot ranges' do node = @attr.not_in 1..3 - compile(node).must_be_like %{ - "users"."id" < 1 OR "users"."id" > 3 - } + compile(node).must_equal( + %{("users"."id" < 1 OR "users"."id" > 3)} + ) end it 'can handle three dot ranges' do node = @attr.not_in 1...3 - compile(node).must_be_like %{ - "users"."id" < 1 OR "users"."id" >= 3 - } + compile(node).must_equal( + %{("users"."id" < 1 OR "users"."id" >= 3)} + ) end it 'can handle ranges bounded by infinity' do -- cgit v1.2.3