instance method
not
Ruby on Rails 6.1.7.10
Since v4.0.13Signature
not(opts, *rest)
Returns a new relation expressing WHERE + NOT condition according to the conditions in the arguments.
#not accepts conditions as a string, array, or hash. See QueryMethods#where for more details on each format.
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")
# SELECT * FROM users WHERE name != 'Jon'
User.where.not(name: nil)
# SELECT * FROM users WHERE name IS NOT NULL
User.where.not(name: %w(Ko1 Nobu))
# SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
User.where.not(name: "Jon", role: "admin")
# SELECT * FROM users WHERE NOT (name == 'Jon' AND role == 'admin')
Parameters
-
optsreq -
restrest
Source
# File activerecord/lib/active_record/relation/query_methods.rb, line 45
def not(opts, *rest)
where_clause = @scope.send(:build_where_clause, opts, rest)
@scope.where_clause += where_clause.invert
@scope
end
Defined in activerecord/lib/active_record/relation/query_methods.rb line 45
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::QueryMethods::WhereChain