activerecord-postgres_enum

Usage

Migrations

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:

drop_enum :mood

To rename an existing enum:

rename_enum :mood, :emotions

To add a value into existing enum:

add_enum_value :mood, "pensive"

To remove a value from existing enum:

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

remove_enum_value :mood, "pensive"

To add a new enum column to an existing table:

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

  add_column :products, :type, :product_type
end

To rename a value:

rename_enum_value :mood, "pensive", "wistful"
Author

In the same orbit

Explore more open source projects

Contact us

We’d love to hear from you! We’re not really all that evil, and we love discussing potential projects, intriguing ideas, and new opportunities. Complete the form below or drop us a line at surrender@evilmartians.com.

Martians at a glance
16
years in business

A product development consultancy that works with startups and established businesses, while also creating open source-based products and services