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 12, 2017 by Lucas Serrano's avatar Lucas Serrano
Hide whitespace changes
Inline Side-by-side
Enum.md
View page @ 626aa3d4
Enum type manager. It creates a separated class to hold each enum set that can be used by multiple models, it also keeps the database data consistent. The enum type is known to have a better performance against string- and integer-like enums. [PostgreSQL Docs](https://www.postgresql.org/docs/9.2/static/datatype-enum.html)
Enum type manager. It creates a separated class to hold each enum set that can be used by multiple models, it also keeps the database consistent. The enum type is known to have better performance against string- and integer-like enums. [PostgreSQL Docs](https://www.postgresql.org/docs/9.2/static/datatype-enum.html)
# How it works
......@@ -34,7 +34,7 @@ add_enum_values :status, %i(baz qux), suffix: 'tst' # With a specific suff
drop_type :status
```
Once you've created the type, you can use while creating your table in three ways
Once you've created the type, you can use it while creating your table in three ways
```ruby
create_table :users do |t|
t.string :name
......@@ -46,7 +46,7 @@ end
### The type class
Each enum type that is loaded from the database will have it's own class type of value, created under the [`enum.namespace`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.namespace) namespace.
Each enum type loaded from the database will have it's own class type of value, created under the [`enum.namespace`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.namespace) namespace.
```ruby
Enum::Roles
......@@ -56,19 +56,19 @@ Enum::Roles.admin
# Or you can get the list of values
Enum::Roles.values
# Allows you to iterate over the values direct from the class
# Allows you to iterate over the values directly from the class
Enum::Roles.each do |role|
puts role
end
# You can use index-based reference of a value
# You can use index-based references of a value
Enum::Roles.new(0) # #<Enum::Roles "visitor">
Enum::Roles.admin.to_i # 2
```
### Models
If you kept the [`enum.initializer`](https://github.com/crashtech/torque-postgresql/wiki/Enum#enum.initializer) setting as `false`, you have to go to each of your models and enable the functionality for each enum type of 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/Enum#enum.base_method).
If you kept the [`enum.initializer`](https://github.com/crashtech/torque-postgresql/wiki/Enum#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/Enum#enum.base_method).
```ruby
# models/user.rb
class User < ActiveRecord::Base
......
Clone repository
  • Wiki
  • Configuring

Data types

  • Enum
  • Interval

Querying

  • Distinct On
  • Auxiliary Statements