How do I describe an enumeration column in a Rails 3 migration? How do I describe an enumeration column in a Rails 3 migration? ruby ruby

How do I describe an enumeration column in a Rails 3 migration?


Rails 4.1 contains enum for now!

You can write just

class User < ActiveRecord::Base  enum status: [ :admin, :user, :banned ]end

For migration write

t.integer :status

Rails 3 & 4.0

Best solution in my opinion is simple_enum gem.


In a Rails 3 Migration you can do the following:

class CreateFoo < ActiveRecord::Migration  def change    create_table :foo do |t|      t.column :foobar, "ENUM('foo', 'bar')"    end  endend

This will create the table with the single column "foobar" and the values.


You can describe an enumeration column with:

t.column 'role', 'user_role'

I created the enum type with:

execute "CREATE TYPE user_role AS ENUM ('consultant', 'admin');"

Inspecting the database:

    Column     |          Type          | Modifiers | Storage  | Stats target | Description---------------+------------------------+-----------+----------+--------------+------------- role          | user_role              |           | plain    |              |