Rails 4 Validation: where to put the allow_nil when doing an inclusion?

泄露秘密 提交于 2019-12-11 07:44:21

问题


Are these two implementations functionally equivalent? If so, which is "better?"

  # from a model
  WIDGET_COLORS = %w(red yellow green)
  validates :widget_color,
           inclusion: {in: WIDGET_COLORS, allow_nil: true}

or

  # from a model
  WIDGET_COLORS = %w(red yellow green)
  validates :widget_color,
           inclusion: {in: WIDGET_COLORS},
           allow_nil: true

UPDATE: fixed typo so example reads validates


回答1:


Firstly validate and validates are different methods - it should be validates here.

validates will search the supplied hash for so-called _validates_default_keys, which is an internal array [:if, :unless, :on, :allow_blank, :allow_nil , :strict]. All the arguments passed to validates being in this array are treated as common options for all the validators attached to the model with this method. So if you do:

validates :widget_color,
          inclusion: {in: WIDGET_COLORS},
          uniqueness: true,
          allow_nil: true

allow_nil will affect both of the validators, or is equivalent of:

validates :widget_color,
          inclusion: {in: WIDGET_COLORS, allow_nil: true},
          uniqueness: {allow_nil: true}

On the other hand with

validates :widget_color,
          inclusion: {in: WIDGET_COLORS, allow_nil: true},
          uniqueness: true

it will only affect the validator it is defined for (in this case InclusionValidator)



来源:https://stackoverflow.com/questions/26770359/rails-4-validation-where-to-put-the-allow-nil-when-doing-an-inclusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!