method

unscope

Importance_2
unscope(*args) public

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)

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:

Post.comments.except(:order)

will still have an order if it comes from the default_scope on Comment.

Show source
Register or log in to add new notes.