Class: ActiveRecord::QueryMethods::WhereChain
- Inherits:
-
Object
- Object
- ActiveRecord::QueryMethods::WhereChain
- Includes:
- ActiveModel::ForbiddenAttributesProtection
- Defined in:
- lib/active_record/relation/query_methods.rb
Overview
WhereChain objects act as placeholder for queries in which #where does not have any parameter. In this case, #where must be chained with #not to return a new relation.
Instance Method Summary collapse
-
#initialize(scope) ⇒ WhereChain
constructor
A new instance of WhereChain.
-
#not(opts, *rest) ⇒ Object
Returns a new relation expressing WHERE + NOT condition according to the conditions in the arguments.
Constructor Details
#initialize(scope) ⇒ WhereChain
Returns a new instance of WhereChain.
18 19 20 |
# File 'lib/active_record/relation/query_methods.rb', line 18 def initialize(scope) @scope = scope end |
Instance Method Details
#not(opts, *rest) ⇒ Object
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 name != 'Jon' AND role != 'admin'
45 46 47 48 49 50 51 52 53 |
# File 'lib/active_record/relation/query_methods.rb', line 45 def not(opts, *rest) opts = sanitize_forbidden_attributes(opts) where_clause = @scope.send(:where_clause_factory).build(opts, rest) @scope.references!(PredicateBuilder.references(opts)) if Hash === opts @scope.where_clause += where_clause.invert @scope end |