How do I describe an enumeration column in a Rails 3 migration?
11 Answers
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.

- 14,134
- 7
- 65
- 142
-
3Unfortunately the values are stored as integers not as ENUM with several downsides: http://hn.meteor.com/posts/6925010-32d11 – migu Feb 24 '14 at 11:16
-
2@migu this is de-facto standard of rails enum. `simple_enum` gem works the same. See updated answer – Alex Antonov Apr 29 '14 at 13:18
-
2@migu Can you double check that link - I'm super interested in it but it no worky for me. :) Thanks. – Joshua Pinter Nov 05 '14 at 21:09
-
They asked about rails 3, not 4 – f0ster Jan 27 '15 at 19:20
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
end
end
This will create the table with the single column "foobar" and the values.

- 91
- 1
- 1
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 | |

- 3,957
- 1
- 31
- 27
This will also work....
add_column :table_name, :column_name, "enum('abc','def','ghi')", :default => 'abc'

- 1,150
- 10
- 15
Something like
class User < ActiveRecord::Base
validates_inclusion_of :status, :in => [:active, :inactive]
def status
read_attribute(:status).to_sym
end
def status= (value)
write_attribute(:status, value.to_s)
end
end

- 44,198
- 62
- 180
- 289
-
1
-
2This was pasted from http://stackoverflow.com/questions/693928/how-replacecreate-an-enum-field-on-rails-2-0-migrations – Jeriko Nov 26 '10 at 07:54
-
3This was pasted from http://zargony.com/2008/04/28/five-tips-for-developing-rails-applications – ceth Nov 26 '10 at 08:00
I like enumerated_attribute gem: https://github.com/jeffp/enumerated_attribute
Easy enum for your models, objects and views.
Works fine with rails 3.1
What worked for me was mapping it from symbols to integers
TYPE_MAP = { type_one: 1, type_two:2, another_type:3 }
def type
TYPE_MAP.key(read_attribute(:type))
end
def type=(s)
write_attribute(:type, TYPE_MAP[s])
end
But for the controller you have to map it again like this:
def create
@cupon_type = CuponType.new(params[:cupon_type])
@cupon_type.type = params[:cupon_type][:type].to_sym
Note the .to_sym that overrides the first creation on the object (in my case it was coupons).
Now you can use it like this:
c.type == :type_one
c.type = :type_two

- 107
- 8
-
You can add `.with_indifferent_access` to the `TYPE_MAP` to allow for string access – Chris Aug 23 '15 at 23:04
I'll use the enum_fu gem: https://github.com/ikspres/enum_fu

- 1,750
- 2
- 14
- 25
-
4Has not been updated since 2009. Does not seem to be actively maintained. – phatmann Oct 24 '11 at 12:56
t.enum :file_type ,:limit => [:jpg, :png, :gif] ,:default => :gif

- 295
- 1
- 2
- 8
-
1what plugin are you using for this? enum is not supported by default – Michael Andersen Feb 03 '11 at 14:23
-
2script/plugin install svn://rubyforge.org/var/svn/enum-column/plugins/enum-column – HeeL May 19 '11 at 13:29