simple_enum

Guide for setting up and using simple_enum with Rails and ActiveRecord.

← Back

Using simple_enum with Rails

If you've added simple_enum to your Gemfile and you are using ActiveRecord, simple_enum works out of to box. Enumeration columns can then be specified with either an Array or Hash.

  1. Add a code column to hold the enum value to your table using migrations:
    # Colum names should be named like {{enum_name}}_cd
    add_column :users, :gender_cd, :integer
    
  2. Add as_enum to your model:
    class User < ActiveRecord::Base
      # Using a Hash (recommended):
      as_enum :gender, :female => 1, :male => 0
    
      # OR using an Array:
      # as_enum :gender, %w{male female}
    end
    
  3. Yeah, done!
    person = Person.new(:gender => :female)
    person.female?   # => true
    person.gender    # => :female
    person.gender_cd # => 1
    

Custom column names

Changing the source for an enum can easily be achived by using the :column option for as_enum, using this it's possible to use arbitary column names.

Note: it is not valid to use a column name which has the same name as the enum.

class User < ActiveRecord::Base
  # OK:
  as_enum :gender, %w{male female}, :column => 'sex'

  # BAD (throws ArgumentError):
  # as_enum :sex, %w{male female}, :column => 'sex'
end