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

RubyRuby on-Rails-3EnumsMigration

Ruby Problem Overview

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

Ruby Solutions

Solution 1 - Ruby

Rails 4.1 contains enum for now!

You can write just

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

For migration write

t.integer :status

Rails 3 & 4.0

Best solution in my opinion is simple_enum gem.

Solution 2 - Ruby

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')"

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

Solution 3 - Ruby

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

Solution 4 - Ruby

Something like

class User < ActiveRecord::Base
   validates_inclusion_of :status, :in => [:active, :inactive]

   def status

   def status= (value)
     write_attribute(:status, value.to_s)

Solution 5 - Ruby

I like enumerated_attribute gem:

Easy enum for your models, objects and views.

Works fine with rails 3.1

Solution 6 - Ruby

This will also work....

add_column :table_name, :column_name, "enum('abc','def','ghi')", :default => 'abc'

Solution 7 - Ruby

I'll use the enum_fu gem:

Solution 8 - Ruby

What worked for me was mapping it from symbols to integers

TYPE_MAP = { type_one: 1, type_two:2, another_type:3 }

def type

def type=(s)
	write_attribute(:type, TYPE_MAP[s])

But for the controller you have to map it again like this:

 def create
  @cupon_type =[: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

Solution 9 - Ruby

Have a look at active_enum.

I think it fits your needs.

Solution 10 - Ruby

Use enum_column to add enum support to active record

Solution 11 - Ruby

t.enum :file_type ,:limit => [:jpg, :png, :gif] ,:default => :gif


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZeckView Question on Stackoverflow
Solution 1 - RubyasiniyView Answer on Stackoverflow
Solution 2 - RubyamerdiditView Answer on Stackoverflow
Solution 3 - RubyMoriartyView Answer on Stackoverflow
Solution 4 - RubycethView Answer on Stackoverflow
Solution 5 - Rubyuser1085302View Answer on Stackoverflow
Solution 6 - Rubybigtex777View Answer on Stackoverflow
Solution 7 - RubyPastaView Answer on Stackoverflow
Solution 8 - RubyDanielZivView Answer on Stackoverflow
Solution 9 - RubyPikachuView Answer on Stackoverflow
Solution 10 - RubyMohsen AlizadehView Answer on Stackoverflow
Solution 11 - RubyHeeLView Answer on Stackoverflow