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

后端 未结 10 1642
轮回少年
轮回少年 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:39

    You can try the (very) comprehensive jeff's enumerated_attribute gem OR go with this simple workaround:

    class Person < ActiveRecord::Base
      SEX = [:male, :female]
    
      def sex
        SEX[read_attribute(:sex)]
      end
    
      def sex=(value)
        write_attribute(:sex, SEX.index(value))
      end
    end
    

    And then declare the sex attribute as an integer:

    t.integer :sex
    

    This worked very fine for me! =D

提交回复
热议问题