I'm a React and RoR enthusiast with over two years of experience creating innovative software solutions that improve business productivity. I work well in teams and am always up for challenging projects. Born and raised in Pune, I enjoy cooking, painting and travelling in my free time.
2 minute read
We often come across cases in our Rails application where we want to negate our
where clause conditions.
Let’s say we have a system that requires users to verify both their email and phone number.
We add two columns email_verified
and phone_verified to our User model for verifying the account.
A user is verified only if both the email and phone of the user are verified by the system.
Before
Before Rails 7,
we would add verified and unverified scopes to our User model
to get the details of verified and unverified users in our system.
As seen above, we have a verified scope that checks if email_verified
and phone_verified columns are true.
But to fetch unverified users we had to introduce another scope with the where.not clause.
This exposes the where.not method which can be kept internal to Rails code.
Similarly, we have added separate scopes for users with verified and unverified emails.
After
Rails 7
adds#invert_where method to ActiveRecord that will invert all scope conditions.
Instead of creating unverified and with_unverified_email scopes with negating conditions,
we can just chain invert_where to verified and with_verified_email scopes respectively as shown below.