... | @@ -62,13 +62,13 @@ Enum::Roles.each do |role| |
... | @@ -62,13 +62,13 @@ Enum::Roles.each do |role| |
|
end
|
|
end
|
|
|
|
|
|
# You can use index-based references of a value
|
|
# 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
|
|
Enum::Roles.admin.to_i # 2
|
|
```
|
|
```
|
|
|
|
|
|
### Models
|
|
### 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
|
|
```ruby
|
|
# models/user.rb
|
|
# models/user.rb
|
|
class User < ActiveRecord::Base
|
|
class User < ActiveRecord::Base
|
... | @@ -84,9 +84,9 @@ User.roles |
... | @@ -84,9 +84,9 @@ User.roles |
|
You can set the column value from `String`, `Symbol`, and `Integer`:
|
|
You can set the column value from `String`, `Symbol`, and `Integer`:
|
|
```ruby
|
|
```ruby
|
|
user = User.new
|
|
user = User.new
|
|
user.role = 'admin' # #<Enum::Roles "admin">
|
|
user.role = 'admin' # :admin
|
|
user.role = :manager # #<Enum::Roles "manager">
|
|
user.role = :manager # :manager
|
|
user.role = 0 # #<Enum::Roles "visitor">
|
|
user.role = 0 # :visitor
|
|
```
|
|
```
|
|
|
|
|
|
This allows you to compare values, or make questions about it:
|
|
This allows you to compare values, or make questions about it:
|
... | | ... | |