... | @@ -11,12 +11,15 @@ create_enum :roles, %i(visitor manager admin) |
... | @@ -11,12 +11,15 @@ create_enum :roles, %i(visitor manager admin) |
|
|
|
|
|
Some other examples are:
|
|
Some other examples are:
|
|
```ruby
|
|
```ruby
|
|
|
|
# Only for Rails < 7.0
|
|
# ['status_foo', 'status_bar']
|
|
# ['status_foo', 'status_bar']
|
|
create_enum :status, %i(foo bar), prefix: true
|
|
create_enum :status, %i(foo bar), prefix: true
|
|
# 'foo_tst', 'bar_tst']
|
|
# 'foo_tst', 'bar_tst']
|
|
create_enum :status, %i(foo bar), suffix: 'tst'
|
|
create_enum :status, %i(foo bar), suffix: 'tst'
|
|
```
|
|
```
|
|
|
|
|
|
|
|
> NOTE: The `create_enum` method now exists on Rails, so it was removed from the gem, preventing the use of prefix and suffix. However, the `add_enum_values` still exists only in this gem.
|
|
|
|
|
|
You can also manage this type along other migrations, renaming, adding values, or deleting it.
|
|
You can also manage this type along other migrations, renaming, adding values, or deleting it.
|
|
```ruby
|
|
```ruby
|
|
# Rename enum by renaming the type it represents
|
|
# Rename enum by renaming the type it represents
|
... | @@ -38,9 +41,10 @@ Once you've created the type, you can use it while creating your table in three |
... | @@ -38,9 +41,10 @@ Once you've created the type, you can use it while creating your table in three |
|
```ruby
|
|
```ruby
|
|
create_table :users do |t|
|
|
create_table :users do |t|
|
|
t.string :name
|
|
t.string :name
|
|
t.role :role # Uses the type of the column, since enum is a type
|
|
t.role :role # Uses the type of the column, since enum is a type
|
|
t.enum :status # Figures the type name from the column name
|
|
t.enum :status # Figures the type name from the column name !! ONLY RAILS < 7.0
|
|
t.enum :last_status, subtype: :status # Explicit tells the type to be used
|
|
t.enum :last_status, subtype: :status # Explicit tells the type to be used !! ONLY RAILS < 7.0
|
|
|
|
t.enum :last_status, enum_type: :status # This is the official Rails 7.0 syntax !! ONLY RAILS >= 7.0
|
|
end
|
|
end
|
|
```
|
|
```
|
|
|
|
|
... | @@ -93,7 +97,7 @@ You have to go to each of your models and enable the functionality for each enum |
... | @@ -93,7 +97,7 @@ You have to go to each of your models and enable the functionality for each enum |
|
```ruby
|
|
```ruby
|
|
# models/user.rb
|
|
# models/user.rb
|
|
class User < ActiveRecord::Base
|
|
class User < ActiveRecord::Base
|
|
enum :role, :status
|
|
torque_enum :role, :status
|
|
end
|
|
end
|
|
```
|
|
```
|
|
|
|
|
... | | ... | |