# Creating easy, readable attributes with ActiveRecord enums

DevFeed: [Creating easy, readable attributes with ActiveRecord enums](<https://devfeed.tech/articles/creating-easy-readable-attributes-with-activerecord-enums-26267.md>)

Original publisher: [Read original article](<https://www.justinweiss.com/articles/creating-easy-readable-attributes-with-activerecord-enums/>)

Author: Justin Weiss

Published: 2015-06-30T07:39:15Z

Content type: tutorial

Language: en

Sources: [Justin Weiss](<https://devfeed.tech/sources/justin-weiss.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [enum](<https://devfeed.tech/topics/enum.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Code](<https://devfeed.tech/topics/code.md>), [Database](<https://devfeed.tech/topics/database.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [code](<https://devfeed.tech/tags/code.md>), [database](<https://devfeed.tech/tags/database.md>), [enum](<https://devfeed.tech/tags/enum.md>), [enums](<https://devfeed.tech/tags/enums.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

A tutorial on Rails ActiveRecord enums for attributes with a small, mostly fixed set of values. It explains how enums map readable values to database integers, provide query and predicate methods, and introduce maintenance concerns such as ordering and sharing mappings outside Rails.

## Source excerpt

Imagine a question that can be either "pending", "approved", or "flagged". Or a phone number that's a "home", "office", "mobile", or "fax" (if it's 1982). Some models call for this kind of data. An attribute that can have only one of a few different values. And that set of values almost never changes. It's a situation where, if it were plain Ruby, you'd just use a symbol. You could create a PhoneNumberType or QuestionStatus model and a belongs_to relationship to hold these values, but that doesn't seem worth it. You could stuff them in a yaml file, but now you have to look in a totally different place to figure out what your object can do. In 4.1, Rails took a stab at solving this problem with ActiveRecord enums. A few values, in the model ActiveRecord enums are pretty easy. You give your model an integer column: bin/rails g model phone number:string phone_number_type:integer List the values that attribute can take: app/models/phone.rbclass Phone < ActiveRecord::Base enum phone_number_type: [:home, :office, :mobile, :fax] end And now you can deal with strings instead of numbers. Instead of this: irb(main):001:0> Phone.first.phone_number_type => 3 You'll see this: irb(main):002:0> Phone.first.phone_number_type => "fax" You can change that attribute using either strings or ints: irb(main):003:0> phone.phone_number_type = 1; phone.phone_number_type => "office" irb(main):004:0> phone.phone_number_type = "mobile"; phone.phone_number_type => "mobile" Or even using a bang method: irb(main):005:0> phone.office! => true irb(main):006:0> phone.phone_number_type => "office" You get methods for asking if your attribute has some specific value: irb(main):007:0> phone.office? => true And you can find all objects with the value you're looking for: irb(main):008:0> Phone.office Phone Load (0.3ms) SELECT "phones".* FROM "phones" WHERE "phones"."phone_number_type" = ? [["phone_number_type", 1]] If you want to see all the different values you can use, along with the numbers they're as