A Simple Guide to Enum’s with Rails


An enum is a special data type that allows you to declare and query data with string variables but stores an integer in the database. You can think of an enum as being able to set a variable to a value (for Ruby on Rails, its an integer value). For example, if you want a status column in your database model, you could set the status to enum type and store your possible statuses started, in progress, and complete, as 0, 1, and 2.


Not only does this let developers write more semantic code, but it’s also more space-efficient, less bug-prone, and easy to update down the road!


I like to think of an enum as a combination of a boolean and a string. If you have an attribute that has a finite number of possible values, that will likely have to be updated, this could be a good place to use an enum. Here are some examples:

  • Status enum (not started: 0, in progress: 1, complete: 2)
  • Direction enum (north: 0, south: 1, east: 2, west: 3)
  • Suit enum (hearts: 0, spades: 1, diamonds: 2, clubs: 3)


In Ruby on Rails

Setting up an enum

  1. Set up your model with the enum attribute set to an integer
rails g model Task description:string status:integer

2. In the model file, list the characteristics of the attribute. Note that you can let Rails set default values but can declare the values as well.

class Task < ActiveRecord::Base
enum status: [ :not_started, :in_progress, :complete ]
class Task < ActiveRecord::Base
enum status: {in_progress: 1 , complete: 2, not_started: 0 }
// both ways set "not_started" = 0, "in_progress" = 1, "complete" = // 2

That’s it! Rails makes it super easy to set up an enum.

Working with an enum

  • Declare or change attributes using the string, the int, even a bang
> task.status = 1; task.status
=> "in_progress"
> task.status = "in_progress"; task.status
=> "in_progress"
> task.in_progress!
=> true
> task.status
=> "in_progress"
  • Get the value or get whether the instance is a specific status
> task.status
=> "in_progress"
> task.first.in_progress?
=> true
  • Find all objects with a specific value
> Task.in_progress
TaskLoad (0.3ms) SELECT "tasks".* FROM "tasks" WHERE "taskes"."status" = ? [["status", 1]]
  • Enums can make writing forms a breeze
// ../form.html.erb<div class="field">
<%= f.label :status %><br>
<%= f.select :status, Phone.status.keys %>

As always, thanks for reading!! For more on Enums check out some of the resources below 📚

Full-Stack Software Engineer, Designer, and salsa dancer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

You’re Doing Scrum Wrong — Here’s How To Do It Right

Work meeting

Flutter State Management: setState, BLoC, ValueNotifier, Provider

FLOC Origin Trial: What We Observed (1/4)

Why you should learn python in 2020

Fortune Wheel Update is live!

Hello World Django

Hello World Django

How Behaviour Driven Development improves software quality and collaboration in the development…

Why Choose Flutter in 2022?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lydia Gregory

Lydia Gregory

Full-Stack Software Engineer, Designer, and salsa dancer.

More from Medium

Top 3 Coding Tutorials of the Week

Devise User Management

Ruby Will Make You Happy: An Introduction to a Developer’s Best Friend

“Floating” in Rubies.