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.
Use enum_column to add enum support to active record
https://github.com/mdsol/enum_column
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
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
I'll use the enum_fu gem: https://github.com/ikspres/enum_fu
Have a look at active_enum.
I think it fits your needs.