When enums are used in a model in an Rails application, it generates corresponding scopes in order to easily query or find out if record belongs to the particular enum.
Let’s take an example.
Before Rails 6
As we can see, status of the Product can either be active, inactive or archived. When we use it with enums, it generates scopes on ActiveRecord as given below.
ActiveRecord enum scopes on collection
This can be used to get active products with the scope generated by enums defined on the model.
After Rails 6
In addition to scopes defined above, we have access to negative scopes as given below.
As we can see, the scopes generated is a short hand for
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox