Usually the field 'kind' should be allowed blank. but if it is not blank, the value should included in ['a', 'b']
validates_inclusion_of :kind, :in => ['a', 'b'], :allow_nil => true
The code does not work?
Usually the field 'kind' should be allowed blank. but if it is not blank, the value should included in ['a', 'b']
validates_inclusion_of :kind, :in => ['a', 'b'], :allow_nil => true
The code does not work?
In Rails 5 you can use
allow_blank: true
outside or inside inclusion block:or
tip: you can use
in: %w(a b)
for text valuesThis syntax will perform inclusion validation while allowing nils:
check also :allow_blank => true
If you are trying to achieve this in Rails 5 in a
belongs_to
association, consider that the default behaviour requires the value to exist.To opt out from this behaviour you must specify the
optional
flag: