Rails enum negative scope (where not)

Viewed 6126

I use enum for a column in my users table. I want to be able to find all users who are not pending. This is my current code:

enum approval_status: [:pending, :approved, :declined]

User.where.not(approval_status: :pending)

But the SQL query becomes this:

User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE (`users`.`approval_status` != NULL)

No matter what enum value I put in, the SQL turns it to NULL

How do I get ALL users, EXCEPT for a particular value of the approval_status column?

2 Answers

In Rails 6, negative scopes are added on the enum values.

You can use it like this:

User.not_pending
User.not_approved
User.not_declined

Related pull request

Related