Update Postgres guide for ActiveRecord::Enum support

Updates https://guides.rubyonrails.org/active_record_postgresql.html#enumerated-types based the new features added in https://github.com/rails/rails/pull/41469
This commit is contained in:
Alex Ghiculescu 2022-01-03 10:40:52 -05:00
parent 296ef7a172
commit 5d4309e454
1 changed files with 13 additions and 10 deletions

View File

@ -248,21 +248,20 @@ irb> contact.save!
* [type definition](https://www.postgresql.org/docs/current/static/datatype-enum.html)
Currently there is no special support for enumerated types. They are mapped as
normal text columns:
The type can be mapped as a normal text column, or to an [`ActiveRecord::Enum`](https://api.rubyonrails.org/classes/ActiveRecord/Enum.html).
```ruby
# db/migrate/20131220144913_create_articles.rb
def up
execute <<-SQL
CREATE TYPE article_status AS ENUM ('draft', 'published');
SQL
create_enum :article_status, ["draft", "published"]
create_table :articles do |t|
t.column :status, :article_status
t.enum :status, enum_type: :article_status, default: "draft", null: false
end
end
# NOTE: It's important to drop table before dropping enum.
# There's no built in support for dropping enums, but you can do it manually.
# You should first drop any table that depends on them.
def down
drop_table :articles
@ -275,17 +274,21 @@ end
```ruby
# app/models/article.rb
class Article < ApplicationRecord
enum status: {
draft: "draft", published: "published"
}, _prefix: true
end
```
```irb
irb> Article.create status: "draft"
irb> article = Article.first
irb> article.status_draft!
irb> article.status
=> "draft"
irb> article.status = "published"
irb> article.save!
irb> article.status_published?
=> false
```
To add a new value before/after existing one you should use [ALTER TYPE](https://www.postgresql.org/docs/current/static/sql-altertype.html):
@ -302,7 +305,7 @@ def up
end
```
NOTE: ENUM values can't be dropped currently. You can read why [here](https://www.postgresql.org/message-id/29F36C7C98AB09499B1A209D48EAA615B7653DBC8A@mail2a.alliedtesting.com).
NOTE: Enum values can't be dropped. You can read why [here](https://www.postgresql.org/message-id/29F36C7C98AB09499B1A209D48EAA615B7653DBC8A@mail2a.alliedtesting.com).
Hint: to show all the values of the all enums you have, you should call this query in `bin/rails db` or `psql` console: