instance method
unscope
Ruby on Rails 7.2.3
Since v4.0.13Signature
unscope(*args)
Removes an unwanted relation that is already defined on a chain of relations. This is useful when passing around chains of relations and would like to modify the relations without reconstructing the entire chain.
User.order('email DESC').unscope(:order) == User.all
The method arguments are symbols which correspond to the names of the methods which should be unscoped. The valid arguments are given in VALID_UNSCOPING_VALUES. The method can also be called with multiple arguments. For example:
User.order('email DESC').select('id').where(name: "John") .unscope(:order, :select, :where) == User.all
One can additionally pass a hash as an argument to unscope specific :where values. This is done by passing a hash with a single key-value pair. The key should be :where and the value should be the where value to unscope. For example:
User.where(name: "John", active: true).unscope(where: :name)
== User.where(active: true)
This method is similar to #except, but unlike #except, it persists across merges:
User.order('email').merge(User.except(:order))
== User.order('email')
User.order('email').merge(User.unscope(:order))
== User.all
This means it can be used in association definitions:
has_many :comments, -> { unscope(where: :trashed) }
Parameters
-
argsrest
Source
# File activerecord/lib/active_record/relation/query_methods.rb, line 786
def unscope(*args)
check_if_method_has_arguments!(__callee__, args)
spawn.unscope!(*args)
end
Defined in activerecord/lib/active_record/relation/query_methods.rb line 786
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::QueryMethods