simple_enum

Guide for integrating simple_enum with Mongoid.

← Back

Using simple_enum with Mongoid

Adding an enum field to a Mongoid document using simple_enum requires an additional step and a slightly adopted as_enum method which works with mongoid.

  1. Load mongoid support in an initalizer:
    require 'simple_enum/mongoid'
    # NOTE TO SELF: use autoload in simple_enum, so we can skip this
    # step in the future...
    
  2. Include Mongoid module and add as_enum to your document, a field named gender_cd is added automatically by simple_enum:
    class User
      include Mongoid::Document
      include SimpleEnum::Mongoid
    
      as_enum :gender, :female => 1, :male => 0
    end
    
  3. Wohooo, done!
    user = User.new(:gender => 'male')
    user.male?     # => true