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.
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:
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:
# File activerecord/lib/active_record/relation/query_methods.rb, line 388 def unscope(*args) check_if_method_has_arguments!(:unscope, args) spawn.unscope!(*args) end