Permalink
Browse files

Fix where.not with in clause

Arel::Nodes::In inherits from Arel::Nodes::Equality, so the case
statement was always using the Equality operator for both scenarios,
resulting in a not equal query instead.
  • Loading branch information...
carlosantoniodasilva committed Dec 7, 2012
1 parent ee5999d commit 89ab303d8b78a73cb7d306a026f185986f44aa2a
@@ -20,15 +20,15 @@ def initialize(scope)
# User.where.not(name: nil)
# # SELECT * FROM users WHERE name IS NOT NULL
#
- # User.where.not(name: %(Ko1 Nobu))
+ # User.where.not(name: %w(Ko1 Nobu))
# # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
def not(opts, *rest)
where_value = @scope.send(:build_where, opts, rest).map do |rel|
case rel
- when Arel::Nodes::Equality
- Arel::Nodes::NotEqual.new(rel.left, rel.right)
when Arel::Nodes::In
Arel::Nodes::NotIn.new(rel.left, rel.right)
+ when Arel::Nodes::Equality
+ Arel::Nodes::NotEqual.new(rel.left, rel.right)
when String
Arel::Nodes::Not.new(Arel::Nodes::SqlLiteral.new(rel))
else
@@ -19,7 +19,7 @@ def test_not_null
end
def test_not_in
- expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], %w[hello goodbye])
+ expected = Arel::Nodes::NotIn.new(Post.arel_table[:title], %w[hello goodbye])
relation = Post.where.not(title: %w[hello goodbye])
assert_equal([expected], relation.where_values)
end

2 comments on commit 89ab303

@amatsuda

This comment has been minimized.

Show comment Hide comment
@amatsuda

amatsuda Dec 8, 2012

Member

Oops 💣

Member

amatsuda replied Dec 8, 2012

Oops 💣

@carlosantoniodasilva

This comment has been minimized.

Show comment Hide comment
@carlosantoniodasilva

carlosantoniodasilva Dec 8, 2012

Owner

😄

😄

Please sign in to comment.