Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 861 Bytes

2016-02-16-unscope_unwanted_relation.md

File metadata and controls

32 lines (23 loc) · 861 Bytes
title tip-number tip-username tip-username-profile tip-description
Unscope to remove or modify the unwanted relation
5
logesh
We can remove or modify the unwanted relation that is already defined on a chain of relations. This would be useful when modifying relations without reconstructing entire chain.
User.order(‘email DESC’).unscope(:order) == User.all

We can also call this method with multiple arguments as

User.order(‘email DESC’).select(‘id’).unscope(:order, :select) == User.all

Also, we can unscope :where values like

User.where(name: “mallow”, active: true).unscope(where: :name) == User.where(active: true)

We can use in association definition

has_many :posts, -> { unscope where: :removed }

I hope the above tip will be useful.