Use Enum to Represent User Roles in your Database

Stacie Taylor-Cima
Mar 27, 2018 · 3 min read

If you are building a Rails app that has users, chances are you’re going to want to assign users different roles so that you can allow/restrict access to different functions (authorization).

If you’re new to development, it’s probably most intuitive for you to think: I’ll add the role attribute to the User model and set the value to string, so that I can assign roles using strings like: guest, member, or admin. These thoughts are natural, because you’re a human.

That logic does make sense and would work if you implemented it, but it leaves a lot of room for mistakes to be made. Imagine your working really fast and accidentally set a user’s role to “membr” instead of “member”. Yikes! By using the Ruby Enum class, you’re able to use integers to represent user roles. This narrows down the possibilities of errors.

In this example, we could say:

  • 0 is a guest user
  • 1 is a member user
  • 2 is an admin user

The problem with this is that using integers isn’t really intuitive. We’re humans. We want to represent words — like guest, member, and admin — with strings. Enum allows us to reference the user roles with strings, but store them in the database as integers. Best of both worlds!

Quick guide to adding user roles to your app using Enum:

  • Start by adding the role attribute to the User model with the following command: $ rails g migration AddRoleToUsers role:integer
  • Add the following line to your User model (models/user.rb): enum role: [:standard, :premium, :admin]

Let’s break this line down:

  • enum refers to the Ruby class that we’re using.
  • role: is the attribute that we’re going to treat as an enum.
  • [:standard, :premium, :admin] are the names we’re using to reference the user role. Because this is an array, and the index starts at 0, standard role will be stored as 0 in the database, and so on.
  • Once you save this, you can open your rails console `rails c`, type User.roles, and it will return a key that will show you which integers correlate with which strings.
  • Set a default role for new users by usingafter_initialize callback on your User model (models/user.rb):

Now you’re able to use your human sense to set or verify user roles using strings while keeping them nice and tidy stored as integers in the database.

Want to test it?

After you implement the code above. Head over to your rails console and give this a try:

  • Grab the first user in your database:
  • Now set that user’s role to admin:
  • Check the user’s role and you’ll see that although the role has been stored in the database as 2, Rails will communicate with you like you’re a human!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade