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.
-
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... -
Include Mongoid module and add
as_enumto your document, a field namedgender_cdis added automatically by simple_enum:class User include Mongoid::Document include SimpleEnum::Mongoid as_enum :gender, :female => 1, :male => 0 end -
Wohooo, done!
user = User.new(:gender => 'male') user.male? # => true