Alex Rivera | Logout

how (replace|create) an enum field on rails 2.0 migrations?

Asked 2009-03-29T01:18:08.423
29

I would like to create an enum field at sone migration I'm doing, I tried searching in google but I can't find the way to do it in the migration

the only thing I found was

  t.column :status, :enum, :limit => [:accepted, :cancelled, :pending]

but looks like the above code runs only on rails 1.xxx and since I'm running rails 2.0

this what I tried but it fails

class CreatePayments < ActiveRecord::Migration
  def self.up
    create_table :payments do |t|
      t.string :concept
      t.integer :user_id
      t.text :notes
      t.enum :status, :limit => [:accepted, :cancelled, :pending]

      t.timestamps
    end
  end

  def self.down
    drop_table :payments
  end
end

So, in case that isn't allowed, what do you think could be a good solution? just a text field, and validating from the model?

Edit
Report

2 Answers

3

Have you looked at the enum-column plugin on RubyForge?

answered 2009-04-10T13:11:58.247
2

Add the following:

module ActiveRecord
  module ConnectionAdapters #:nodoc:
    class TableDefinition
      def enum(*args)
        options = args.extract_options!
        column_names = args

        column_names.each { |name| column(name, 'enum', options) }
      end
    end
  end
end

to lib/enum/table_definition.rb and include it in your init.rb.

answered 2009-03-29T10:28:58.147

Your Answer