# activerecord-postgres_enum

> A Ruby gem to integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.

- URL: https://evilmartians.com/opensource/activerecord-postgres-enum
- GitHub: https://github.com/bibendi/activerecord-postgres_enum
- Authors: Misha Merkushin

---

Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations to make PostgreSQL enums and views compatible with Rails' `schema.rb`, respectively.

## Usage

### Migrations

```ruby
create_enum :mood, %w(happy great been_better)

create_table :person do
  t.enum :person_mood, enum_type: :mood
end
```

Running the code above will create a `person` table, with a `person_mood` column of type `mood`. This will also be reflected in `schema.rb`, so `rake db:schema:load` works as expected.

To drop an existing enum:

```ruby
drop_enum :mood
```

To rename an existing enum:

```ruby
rename_enum :mood, :emotions
```

To add a value into existing enum:

```ruby
add_enum_value :mood, "pensive"
```

To remove a value from existing enum:

_Warning:_ make sure that value is not used anywhere in the database.

```ruby
remove_enum_value :mood, "pensive"
```

To add a new enum column to an existing table:

```ruby
def change
  create_enum :product_type, %w[one-off subscription]

  add_column :products, :type, :product_type
end
```

To rename a value:

```ruby
rename_enum_value :mood, "pensive", "wistful"
```
