class ActiveRecord::QueryMethods::WhereChain
def not(opts, *rest)
# SELECT * FROM users WHERE NOT (nullable_country = 'UK')
User.where.not(nullable_country: "UK")
User.create!(nullable_country: nil)
nil values on the nullable column won't be returned.
If there is a non-nil condition on a nullable column in the hash condition, the records that have
# SELECT * FROM users WHERE NOT (name == 'Jon' AND role == 'admin')
User.where.not(name: "Jon", role: "admin")
# SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
User.where.not(name: %w(Ko1 Nobu))
# SELECT * FROM users WHERE name IS NOT NULL
User.where.not(name: nil)
# SELECT * FROM users WHERE name != 'Jon'
User.where.not(name: "Jon")
# SELECT * FROM users WHERE NOT (name = 'Jon')
User.where.not(["name = ?", "Jon"])
# SELECT * FROM users WHERE NOT (name = 'Jon')
User.where.not("name = 'Jon'")
more details on each format.
#not accepts conditions as a string, array, or hash. See QueryMethods#where for
the conditions in the arguments.
Returns a new relation expressing WHERE + NOT condition according to
def not(opts, *rest) where_clause = @scope.send(:build_where_clause, opts, rest) @scope.where_clause += where_clause.invert @scope end