What is the rails way of implementing a min max validator in Rails 3 ?
I have a model, with min_age and max_age attributes.
Age can be in the range of 0..100, but I want also to validate crossing values, I mean that max is greather than or equal to min
{:min_age => 0, :max_age => 0} => true
{:min_age => 0, :max_age => 1} => true
{:min_age => 1, :max_age => 0} => false # max < min
{:min_age => 1, :max_age => 101} => false # out of 0..100 range
Best Answer
Check out the ActiveModel::Validations::NumericalityValidator: RailsAPI NumericalityValidator
spec:
code:
I don't know if you want to validate presence or not, but you would just add that as another key to your validations, e.g.