From ab1bd7b71723c6faa1f328913d131801b7c465f4 Mon Sep 17 00:00:00 2001 From: Filip Bartuzi Date: Wed, 30 Sep 2015 22:58:45 +0100 Subject: [PATCH] Add chapter about prefixes explanation for https://github.com/thetron/mongoid-enum/issues/12 --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 2ef7ad4..d20ceb8 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,29 @@ enum :roles, [:noob, :author, :editor], :multiple => true, :default => [:author, enum :roles, [:noob, :author, :editor], :multiple => true, :default => [] # no default ``` +## Custom field name + +By default enum field name is prefixed with underscore ('_') +```ruby +enum :roles, [:noob, :author, :editor], :multiple => true, :default => [:author, :editor] # two defaults +> User.new.attributes +=> {"_id"=>BSON::ObjectId('549d746563616c2168060000'), "_roles" => [:author, :editor] } +``` + +It's important to be aware of prefix when using scopes/queries + +`User.where(_roles: '[:author, :editor]'` + +You can customize prefix with: + +```ruby +Mongoid::Enum.configure do |config| + config.field_name_prefix = '' +end +``` + +Recommended to put inside `config/initializers/mongoid.rb` + ## Validations Validations are baked in by default, and ensure that the value(s) set in