Techie December 2022
Definition
In Rails, an enum is an attribute where the values map to integers in the database and can be queried by name.
Application Example
Suppose we have the orders table and we want to store the status attribute of an order object as either completed, pending or cancelled. Assuming there’s a status column of type integer in the table, having values 0, 1 and 2:
Defining an enum for the status attribute
Working with scopes
We can filter a query the regular way or by using the enum helper:
Checking for equality
We can check if an order is “completed” by using @order.status == “completed” or the enum helper:
Updating the enum
We can update the enum using the enum names or Rails helpers:
Enum options
1 . Using the prefix or suffix options to make it more intuitive:
2 . Disabling scope helper:
The scopes for status won’t be created as we have provided scopes: false:
3 . Opting for array instead of hash:
You can also use an array to define the enum but it’s not a good idea, because changing the order of the enum values will break the mapping.
Thanks for reading, see you in the next one!