An float subtracted from an integer results in a float. When typecast by ActiveRecord, this is converted to an integer.
validates_numericality_of with :only_integer => true results in a rather obscure error message if a non-integer is present (“is not a number”).
validates_numericality_of uses the attribute value before type cast for its validation.
...
That means if you calculate a value before validation, what is printed out by using the attribute method is different than what validates_numericality_of is using for validation. You need to ensure that the value of attr_name_before_type_cast is an integer!
I just spent way too much time figuring this out.


I have met with similar problem but a little different. I have a virtual attribute on a Model and validates_numericality_of doesn’t work for that because there’s not a attr_before_type_cast method.
James, I ran into that, too. But I fooled validates_numericality_of by creating my own before_type_cast method, which just returned the same thing as the regular version.
Worked great! Now I can validate my virtual attribute.