How to validate numericality and inclusion while still allowing attribute to be nil in some cases?

前端 未结 2 471
感动是毒
感动是毒 2021-01-17 09:44

In a Rails app I have several integer attributes on a model.

A user should be able to create a record and leave these attributes blank.

Or, if the user ente

2条回答
  •  轮回少年
    2021-01-17 10:34

    should be simply:

    validates_numericality_of :a, :only_integer => true, :message => "can only be whole number.", :allow_nil => true
    

    same for the second validation

提交回复
热议问题