Updated July 5, 2023
Definition of Rails Enum
Rails enum or rails enumeration is a data type used in Ruby rails programming. This data type contains a set of named values. When a client needs to create an attribute and needs to assign it to one of the less available values, then enum is used during that time. We can able to define an enum for attributes like status. For this attribute, the possible values are “archived”, “pending” or “active”. In short, enum is a collection of named values in ruby rails programming. The additional feature is that the enums in the parent class can be accessed in their sub-class as well.
What is rails enum?
Enumeration is a process of traversing over objects. The object is enumerable when there is an overlap between an attribute (set of values) and a method. We will get an instance of enumerator if we call each method on an array without specifying a block to run for each of its elements. The process of assigning one attribute to anyone from another few existing values is called as an enum.
Why do we use an enum?
If a client is designing a relational diagram or some network-based diagram, and the client needs some new additional up-gradation to the existing diagram, in other programming languages it is not much easier. Because there will be several compartments or domains present in the diagram. Thus, the model may contain multiple attributes of different types. Here, the requirement would be like assigning one attribute to the other from the existing values. There we need enumeration. Attribute means, a set of values, that is constant. Rails version 4.1 will support the enum data type.
The prime use of enum in rails is the addition of enum to the already available model. For instance, we can compare this process to the addition of a column to an available table. They help to specify types of models. As we discussed before, enums will facilitate the mapping of values to integers. Since enums are attributes or constants, they do not change and have to be defined prior. Thus, we can use enums only we have a large predefined type. To explain more clearly with a live example is that if an item is present in a supermarket, it can be either belong to the fruit or vegetable category and cannot belong to both. In the same way, if an attribute belongs to the same value, it belongs to a specific set of data only.
How to Use rails enum?
Let us see one simple example for creating an enum.
Let us see how to write the same in another way by using hashes.
Hence enums are conventions of rails. The other advantage is that it may help future developers to understand the content you have written.
Some tips for using enums:
1. Enums should be defined using hashes rather than arrays. This allows you to control the integers.
2. When creating an enum, avoid using consecutive integers. Instead of 1, 2, 3…, use 10, 20, 30… This provides additional room for future key/value additions. If the vegetable is 20 and we want to add root vegetable, I’d put it at the beginning of the list, rather than at the end.
Creating rails enum
Let us see a basic example to create a rails enum. In this example, we see how to add an enum to the already available model. The first step will be on creating an appropriate migration. The column that we are going to add should be an integer type of attribute.
After this, we have to declare the enum attribute in the model.
The basic uses of enum:
We can use an enum for the followings:
1. For checking the equality. To check if the customer is presently use the code below.
customer.status == 'present'
2. For updating the enum we have to use the following set of code.
customer.archived! # same as customer.update(status: :archived)
3. Scopes:
I would like to talk about another type of helper which is known as scopes. Basically, we use the query “where”.
Customer.where(status: 'active')
In case, if we are using the query where then we can get the expected outcome in the following manner:
Customer.active
Rails enum model
The enum method in the Rails model is available to you. It accepts a hash with the column name as the key and the second hash with the statuses mapped to integers as the value.
User model:
Let’s play around with the User model and the Status column now that they’re ready to use Rails’ dynamically built methods. We’ll need some test records in the database to run the test. Type the following code into the console:
As you could see in the above code, even though the popularity column shops integer, I become capable of by skip the image because of the value, and it becomes efficaciously stored withinside the database. It’s due to the fact Rails is aware that popularity is an enum, and it replaces the values automatically.
Thus, enum is highly useful when we use attributes. This blog explains various aspects of the Ruby rails enumeration. They have their own advantage as well. Here the drawback will be like, we can use only predefined attributes. Attributes as, we have seen before, are a set of integers. There are user-defined methods for using the ruby rails enum. They are user-defined. Also, we saw about different uses of an enum. Ruby rails Enum can be used only in rails programming. We also saw some tips on using the enum for ruby rails. The other uses of rails are checking the equality, they have their scopes, we can update the enum as well. They are mainly attribute-specific.
Conclusion
In a nutshell, that’s what enums are. They are good for designating types that are available to model instances. They are a property of the framework (Rails), not the language, unlike in another language. However, their integration with Rails offers unique benefits such as automated conversions between strings and integers when saving/loading data.
Recommended Articles
We hope that this EDUCBA information on “Rails Enum” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
- Advantages of Rails
- Rails vs PHP
- Rails Commands
- Ruby vs Ruby On Rails