Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chapter about prefixes #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down