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

后端 未结 10 1631
轮回少年
轮回少年 2020-12-07 10:27

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 f

10条回答
  •  [愿得一人]
    2020-12-07 10:58

    You can manually specify the type by using the t.column method instead. Rails will interpret this as a string column, and you can simply add a validator to the model like Pavel suggested:

    class CreatePayments < ActiveRecord::Migration
      def self.up
        create_table :payments do |t|
          t.string :concept
          t.integer :user_id
          t.text :notes
          t.column :status, "ENUM('accepted', 'cancelled', 'pending')"
    
          t.timestamps
        end    
      end
    
      def self.down
        drop_table :payments
      end
    end
    
    class Payment < ActiveRecord::Base
      validates_inclusion_of :status, :in => %w(accepted cancelled pending)
    end
    

提交回复
热议问题