Using Enum Attributes in Ruby on Rails

2023/07/11
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

Dive into the world of enums to streamline your code and make it more readable in Ruby on Rails. Learn best practices, how to add and manage enum attributes, and take advantage of Rails' built-in features to efficiently manage finite sets of values in your models.

Enums, or enumerations, are a common way to represent options for an attribute of a model in Rails. Instead of using strings or creating additional tables, Rails allows you to represent these options as integers and automatically convert them to strings using built-in methods. This makes presenting options to users as strings while storing them as numbers straightforward and maintainable.

In addition to simplifying data representation and storage, enums also come with useful utility methods. Rails automatically generates scope methods for each value, making it easy to query and filter records based on the enum attribute. It also generates methods for each enum value, allowing the code to easily check the current value of an enum attribute in a more readable and expressive manner. Enums are often leveraged in form helpers, making presenting options in a form straightforward.

To add an enum attribute to an existing table in Rails, start by creating a new migration. For example, if you want to add an attribute "status" to the model "course," you can run the following command:

rails generate migration AddStatusToCourses status:integer

If you want the attribute to have a default value, you can open the newly created migration in the db/migrate directory and edit the add_column method call to include the default value you want:

add_column :courses, :status, :integer, default: 0

To apply the migration to your database, run the following command:

rails db:migrate

Next, edit your model to declare the enum and its mappings of strings to integers:

class Course < ApplicationRecord
  enum status: { active: 0, archived: 1, draft: 2 }
end

That's it! You've added an attribute to your model and defined it as an enum. Now you can use the enum attribute in your application.

If you don't already have a model to which you'd like to add an enum, creating a new enum attribute is as easy as any other attribute. Simply generate a new model and define the enum attribute in the migration:

rails generate model Course name:string status:integer

Open up the generated migration and ensure it looks like this:

class CreateCourses < ActiveRecord::Migration[6.0]
  def change
    create_table :courses do |t|
      t.string :name
      t.integer :status

      t.timestamps
    end
  end
end

Finally, run the migration to create the new table:

rails db:migrate

Now you have a new table with an enum attribute ready to be used in your Rails application. Enums provide a powerful and efficient way to manage finite sets of values, making your code more readable and maintainable.