class ActiveRecord::QueryMethods::WhereChain
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.
Public Class Methods
new(scope)
click to toggle source
# File lib/active_record/relation/query_methods.rb, line 20 def initialize(scope) @scope = scope end
Public Instance Methods
not(opts, *rest)
click to toggle source
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')
# File lib/active_record/relation/query_methods.rb, line 44 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 if not_behaves_as_nor?(opts) ActiveSupport::Deprecation.warn(<<~MSG.squish) NOT conditions will no longer behave as NOR in Rails 6.1. To continue using NOR conditions, NOT each condition individually (`#{ opts.flat_map { |key, value| if value.is_a?(Hash) && value.size > 1 value.map { |k, v| ".where.not(#{key.inspect} => { #{k.inspect} => ... })" } else ".where.not(#{key.inspect} => ...)" end }.join }`). MSG @scope.where_clause += where_clause.invert(:nor) else @scope.where_clause += where_clause.invert end @scope end
Private Instance Methods
not_behaves_as_nor?(opts)
click to toggle source
# File lib/active_record/relation/query_methods.rb, line 74 def not_behaves_as_nor?(opts) return false unless opts.is_a?(Hash) opts.any? { |k, v| v.is_a?(Hash) && v.size > 1 } || opts.size > 1 end