Use validates_numericality_of validation. The validation check by default checks the type of float, you need to say that you want to see integers. Since the quantity will not be stored in db, it must be virtual.
Try the following:
attr_accessor :quantity validates_numericality_of :quantity, :only_integer => true
validates_numericality does not accept nil by default, you do not need to check for an attribute, and since you can change the range of quantities in the view, I would not confirm it here.
That you want to check the range, declare it as a constant in the model. Refer to this constant in both validation and presentation.
fifigyuri
source share