Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Wiki
  • Enum

Enum · Changes

Page history
Updated Enum (markdown) authored Apr 01, 2022 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Enum.md
View page @ ebb31868
...@@ -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
``` ```
......
Clone repository
  • Wiki
  • Configuring

Data types

  • Enum
  • Enum Set
  • Interval
  • Date/Time Range
  • Box
  • Circle
  • Line
  • Segment

Querying

  • Arel
  • Has Many
  • Belongs to Many
  • Dynamic Attributes
  • Distinct On
  • Insert All
  • Auxiliary Statements
  • Inherited Tables