I have about 10 attributes in a table that take a number as a value, for each attributes I'd like to validate whether or not a number between 1 - 100 are passed into an input. Any thoughts I can reduce the code below to a one method validation?
Currently, for each attribute, I am doing this;
validates_numericality_of :valueone, :less_than_or_equal_to => 100
validates_numericality_of :valuetwo, :less_than_or_equal_to => 100
validates_numericality_of :valuethree, :less_than_or_equal_to => 100
etc.
Any help is much appreciated. Thanks.
I think you should use constants. It will produce a more elegant code like so:
class Whatever
MIN = 1
MAX = 100
validates :valueone, :valuetwo, :valuethree, :length => { :within => MIN..MAX }
end
You can list all attributes with the same validation as above.
This should do it. =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With