rails validation: :allow_nil and :inclusion both needed at the same time
问题 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? 回答1: This syntax will perform inclusion validation while allowing nils: validates :kind, :inclusion => { :in => ['a', 'b'] }, :allow_nil => true 回答2: In Rails 5 you can use allow_blank: true outside or inside inclusion block: validates :kind, inclusion: { in: ['a', 'b'], allow_blank: true }