Check out the ActiveModel::Validations::NumericalityValidator:
RailsAPI NumericalityValidator
spec:
it {
subject.max_age = 10
subject.min_age = 20
subject.should be_invalid
subject.errors[:min_age].should include("must be less than or equal to #{subject.max_age}")
}
code:
validates :min_age, numericality: { greater_than: 0, less_than_or_equal_to: :max_age }
validates :max_age, numericality: { less_than_or_equal_to: 100 }
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.
validates :max_age, numericality: { less_than_or_equal_to: 100 }, presence: true