Skip to content
Sweet enum sugar for your Mongoid documents
Branch: master
Clone or download
Latest commit b57fb61 Nov 10, 2015
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/mongoid Version bump to 0.4.0 Nov 10, 2015
spec Remove database-cleaner. Sep 17, 2015
.autotest Very naughty mega-commit. Added enum and validator specs. Everything … Nov 6, 2013
.rspec Very naughty mega-commit. Added enum and validator specs. Everything … Nov 6, 2013
.travis.yml Merged plainprogrammer-feature-mongoid4 Jul 12, 2014
Gemfile Initial commit Nov 4, 2013
Guardfile Added Guardfile, updated development dependencies Jul 22, 2015
LICENSE.txt Initial commit Nov 4, 2013 Add information on setting default values for enums with multiple val… May 31, 2014
Rakefile Initial commit Nov 4, 2013
mongoid-enum.gemspec Remove database-cleaner. Sep 17, 2015


Build Status Code Climate

Heavily inspired by DHH's ActiveRecord::Enum, this little library is there to help you cut down the cruft in your models and make the world a happier place at the same time.

A single line will get you fields, accessors, validations and scopes, and a few other bits-and-bobs.


Add this to your Gemfile:

gem "mongoid-enum"

And then run bundle install.


class Payment
  include Mongoid::Document
  include Mongoid::Enum

  enum :status, [:pending, :approved, :declined]

Aaaaaaand then you get things like:

payment = Payment.create

# => :pending

# => :approved

# => :false

# => Mongoid::Criteria for payments with status == :approved



Your enum value is stored as either a Symbol, or an Array (when storing multiple values). The actual field name has a leading underscore (e.g.: _status), but is also aliased with its actual name for you convenience.


Your enums will get getters-and-setters with the same name. So using the Payment example above:

payment.status = :declined
# => :declined

And you also get bang(!) and query(?) methods for each of the values in your enum (see this example.


For each enum, you'll also get a constant named after it. This is to help you elsewhere in your app, should you need to display, or leverage the list of values. Using the above example:

# => [:pending, :approved, :declined]


Enum values are automatically validated against the list. You can disable this behaviour (see below).


A scope added for each of your enum's values. Using the example above, you'd automatically get:

Payment.pending # => Mongoid::Criteria
Payment.approved # => Mongoid::Criteria
Payment.declined # => Mongoid::Criteria


Default value

If not specified, the default will be the first in your list of values (:pending in the example above). You can override this with the :default option:

enum :roles, [:manager, :administrator], :default => ""

Multiple values

Sometimes you'll need to store multiple values from your list, this couldn't be easier:

enum :roles, [:basic, :manager, :administrator], :multiple => true

# ...

user = User.create
user.roles << :basic
user.roles << :manager!

user.manager? # => true
user.administrator? # => false
user.roles # => [:basic, :manager]

Since the underlying datatype for storing values is an array, if you need to specify default(s), ensure you use an array:

enum :roles, [:noob, :author, :editor], :multiple => true, :default => [:author, :editor] # two defaults
enum :roles, [:noob, :author, :editor], :multiple => true, :default => [] # no default


Validations are baked in by default, and ensure that the value(s) set in your field are always from your list of options. If you need more complex validations, or you just want to throw caution to the wind, you can turn them off:

enum :status, [:up, :down], :validate => false

Issues and Feature Requests

If you have any problems, or you have a suggestion, please submit an issue (and a failing test, if you can). Pull requests and feature requests are alwasy welcome and greatly appreciated.

You can’t perform that action at this time.