How do i specify and validate an enum in rails?

Now that Rails 4.1 includes enums you can do the following:

class Attend < ActiveRecord::Base
  enum size: [:yes, :no, :maybe]

  validates :size, inclusion: { in: sizes.keys }
end

Which then provides you with a scope (ie: Attend.yes, Attend.no, Attend.maybe), a checker method to see if certain status is set (ie: #yes?, #no?, #maybe?), along with attribute setter methods (ie: #yes!, #no!, #maybe!).

Rails Docs on enums

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)