Validate on inclusion within array of options OR be nil

前端 未结 2 1960
离开以前
离开以前 2021-02-20 02:45

I have a model where I\'d like to restrict input for a field to either be nil or fall within a specified array of values. I can get the inclusion part working, but the all

2条回答
  •  情歌与酒
    2021-02-20 03:15

    the correct form to allow nil to validate while still allowing a limited array of values is the following:

    validates :preferred_language, inclusion: { in: LANGUAGE_OPTIONS, allow_nil: true }
    

    notice how the allow_nil option is inside the inclusion option hash

提交回复
热议问题