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

前端 未结 11 1459
误落风尘
误落风尘 2020-12-24 04:23

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

相关标签:
11条回答
  • 2020-12-24 05:06

    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.

    0 讨论(0)
  • 2020-12-24 05:08

    Use enum_column to add enum support to active record

    https://github.com/mdsol/enum_column

    0 讨论(0)
  • 2020-12-24 05:09

    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

    0 讨论(0)
  • 2020-12-24 05:20

    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
    
    0 讨论(0)
  • 2020-12-24 05:23

    I'll use the enum_fu gem: https://github.com/ikspres/enum_fu

    0 讨论(0)
  • 2020-12-24 05:23

    Have a look at active_enum.

    I think it fits your needs.

    0 讨论(0)
提交回复
热议问题