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:
Note that this method is more generalized than ActiveRecord::SpawnMethods#except because #except will only affect a particular relation’s values. It won’t wipe the order, grouping, etc. when that relation is merged. For example:
# File activerecord/lib/active_record/relation/query_methods.rb, line 352 def unscope(*args) check_if_method_has_arguments!("unscope", args) spawn.unscope!(*args) end