Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails enum negative scope (where not)

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?

like image 959
Mirror318 Avatar asked Mar 17 '16 21:03

Mirror318


1 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

like image 153
demir Avatar answered Sep 28 '22 08:09

demir