... | ... | @@ -62,13 +62,13 @@ Enum::Roles.each do |role| |
|
|
end
|
|
|
|
|
|
# You can use index-based references of a value
|
|
|
Enum::Roles.new(0) # #<Enum::Roles "visitor">
|
|
|
Enum::Roles.new(0) # :visitor
|
|
|
Enum::Roles.admin.to_i # 2
|
|
|
```
|
|
|
|
|
|
### Models
|
|
|
|
|
|
If you kept the [`enum.initializer`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#enum.initializer) setting as `false`, you have to go to each of your models and enable the functionality for each enum-type field. You don't need to provide the values since they will be loaded from the database. The method name is defined on [`enum.base_method`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#enum.base_method).
|
|
|
You have to go to each of your models and enable the functionality for each enum-type field. You don't need to provide the values since they will be loaded from the database. The method name is defined on [`enum.base_method`](https://github.com/crashtech/torque-postgresql/wiki/Configuring#enum.base_method).
|
|
|
```ruby
|
|
|
# models/user.rb
|
|
|
class User < ActiveRecord::Base
|
... | ... | @@ -84,9 +84,9 @@ User.roles |
|
|
You can set the column value from `String`, `Symbol`, and `Integer`:
|
|
|
```ruby
|
|
|
user = User.new
|
|
|
user.role = 'admin' # #<Enum::Roles "admin">
|
|
|
user.role = :manager # #<Enum::Roles "manager">
|
|
|
user.role = 0 # #<Enum::Roles "visitor">
|
|
|
user.role = 'admin' # :admin
|
|
|
user.role = :manager # :manager
|
|
|
user.role = 0 # :visitor
|
|
|
```
|
|
|
|
|
|
This allows you to compare values, or make questions about it:
|
... | ... | |