Enums - inconsistent

I think one of the strong aims behind ActiveRecord enums is to define an interface that decouples your application’s understanding of that field and the raw value in the database. The raw value could be a string, it could be a number, or it could be a database native enum which may not be represented well in Rails.

So let’s assume we have a status field which uses the following values for some arcane technical reason:

[
  213123, // normal
  238129, // special
  989342 // extra_special
]

How would we work with this field? Sprinkling magic numbers all over our app probably isn’t the best way to deal with it, eg.

do_something if record.status == 213123
record.update!(status: 213123)
Record.where(status: 213123)

What we need is a lookup, ie. something that maps 213123 to our concept of normal. And that’s exactly what AR enums do:

enum status {
  normal: 213123, 
  special: 238129, 
  extra_special: 989342
}

Now instead of using raw values, we use the lookup map everywhere else in our application

do_something if record.status == Record.statuses[:normal]
record.update(status: Record.statuses[:normal])
record.where(status: Record.statuses[:normal])

These cases are actually common enough that Rails provides helpers so that you don’t need to type Record.statuses[:normal] every single time

do_something if record.normal?
record.normal!
Record.normal