Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: validate maximum value of integer

In Rails 4, how can you add a model validation for a maximum value on an integer column?

For example, I want to add a validation to the column "age", to have a maximum value of 100.

There are many questions with answers for Rails 3 and earlier. The docs do not mention any way of checking integer values.

like image 491
Don P Avatar asked Feb 01 '15 21:02

Don P


Video Answer


1 Answers

Take a look at the numericality validators.

In your case, something like

validates :age, numericality: { less_than_or_equal_to: 100, only_integer: true }

like image 53
ABMagil Avatar answered Sep 25 '22 17:09

ABMagil