How do i specify and validate an enum in rails?

前端 未结 8 1816
南笙
南笙 2020-12-30 20:15

I currently have a model Attend that will have a status column, and this status column will only have a few values for it. STATUS_OPTIONS = {:yes, :no, :maybe}

1)

8条回答
  •  难免孤独
    2020-12-30 20:51

    Create a globally accessible array of the options you want, then validate the value of your status column:

    class Attend < ActiveRecord::Base
    
      STATUS_OPTIONS = %w(yes no maybe)
    
      validates :status, :inclusion => {:in => STATUS_OPTIONS}
    
    end
    

    You could then access the possible statuses via Attend::STATUS_OPTIONS

提交回复
热议问题