A bug of validates_numericality_of ?


In code like this

validates_numericality_of :price

the price is supposed to be a string in number format but can include other non-digital characters like 'a', since :only_integer is set to false by default, right?

In rails implementation, it use Kernel.Float to complete the work. But seems Kernel.Float(ruby 1.8.6) always throw an exception when the number string contains non-digital characters, such as "123.00a". So in fact there is no diffierences whether the switch :only_integer is turned on or off.

I found in JRuby, the Kernel.Float will accept string that looks like "123.00a" but will finally return a zero.

Do I misunderstand of the :only_integer switch?

Thanks, Jan

only_integer means it will only allow integers. Otherwise, decimal places are allowed. You will need to write a custom method to validate the "numericality" of strings containing alpha characters.

Integer would mean 100 vs 100.0

I'm new to Ruby, so maybe there's something unique, but why would you think 123.00a is a float?

[123, 123.00, -123.00].each do |n|    puts n.is_a?(Float) end

#false #true

Trying to put in a number like 123.00a in that array makes it fail.

Thanks Guys, I got the meaning of only_integer. I found I was misleaded by JRuby's implementation of Kernel.Float - maybe it's a bug of JRuby ...