Using Enums in Ruby on Rails
For Rails developers, using enums with ActiveRecord is always a good choice when you have to manage the state of an object. For example, for an online shop, you may have three states of an order - i.e. received
, dispatched
and delivered
. One option is to add three boolean fields in the database for received
, dispatched
and delivered
, but this approach looks ugly. A cleaner approach is to add a single string column status that includes values of received
, dispatched
and delivered
, then add three methods in the model file.