validates_numericality_of just got a shot of mojo this weekend in the form of some highly useful options.
only_integer and allow_nil. Any desires to do anything slightly more complex meant dropping down into your own validation routine. Now, some of the more common validations one might want on a number parameter have been option-ized, including:
greater_thangreater_than_or_equal_toequal_toless_thanless_than_or_equal_tooddeven
Which allows us to do things such as:
1 2 3 4 |
validates_numericality_of :salary, :greater_than_or_equal_to => 40000 validates_numericality_of :ten, :equal_to => 10 validates_numericality_of :bonus, :less_than => 5000 validates_numericality_of :prime, :odd => true |
So put away that crusty custom validate and plug into these fresh validation options. Guaranteed to make your life easier.
tags: ruby, rubyonrails

Nice!
what about :between => (1970..2038) or :outside => (-10..10) ?
Hey, what about the only even prime, 2? ;) Seriously though, this is a cool addition to validates_numericality_of.
@Eric: I think the first condition can be met using what is provided. The options are stackable, like:
However, outside the range cannot be done, from what I can see. Maybe it would be helpful to add some of the range options (:is, :in, :within, :minimum, :maximum). This might also lend to consistency of terms.
Try this?
validates_inclusion_of :foo, :in => 50..100, :message => “Die Bewertung muss zwischen 50 und 100 liegen”