How do i specify and validate an enum in rails?

前端 未结 8 1857
南笙
南笙 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:55

    You could use a string column for the status and then the :inclusion option for validates to make sure you only get what you're expecting:

    class Attend < ActiveRecord::Base
        validates :size, :inclusion => { :in => %w{yes no maybe} }
        #...
    end
    

提交回复
热议问题