Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 839
Daniel Avatar asked Jun 28 '13 07:06

Daniel


1 Answers

This syntax will perform inclusion validation while allowing nils:

validates :kind, :inclusion => { :in => ['a', 'b'] }, :allow_nil => true 
like image 101
Matt Avatar answered Sep 28 '22 05:09

Matt