rails validation: :allow_nil and :inclusion both needed at the same time rails validation: :allow_nil and :inclusion both needed at the same time ruby ruby

rails validation: :allow_nil and :inclusion both needed at the same time


This syntax will perform inclusion validation while allowing nils:

validates :kind, :inclusion => { :in => ['a', 'b'] }, :allow_nil => true


In Rails 5 you can use allow_blank: true outside or inside inclusion block:

validates :kind, inclusion: { in: ['a', 'b'], allow_blank: true }

or

validates :kind, inclusion: { in: ['a', 'b'] }, allow_blank: true

tip: you can use in: %w(a b) for text values