# Strange numeric validation problem in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/strange-numeric-validation-problem-in-activerecord/126961>\
**Category:** Rails\
**Created:** [January 23, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/strange-numeric-validation-problem-in-activerecord/126961 "2008-01-23T19:36:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 23, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/strange-numeric-validation-problem-in-activerecord/126961/1 "2008-01-23T19:36:27Z")

</div>

Hi,

I am trying to do validation in an ActiveRecord object.

I have a field called replay\_limit. Here is what happens when the web  
page user  
fills in invalid data into replay\_limit (which is declared as an  
integer in Mysql):

I do puts in the validate routine to show its class and value. Note  
that it is a Fixnum,  
and it has a value of 6. Yet, the to\_yaml shows what the user really  
entered: 6#,  
which I would like to access in the validate routine, and return an  
error.

How can things get in this situation? How can I get at the real  
“string” value of  
what the user entered?

self.replay\_limit.class = Fixnum  
self.replay\_limit = 6

— &id001 !ruby/object:Service  
attributes:  
activation\_pin: “32990”  
sms\_text: “”  
service\_options: 32  
moderator\_id: “0”  
replay\_limit: 6#

Also, why isn’t there quotes around the 6# that to\_yaml outputs?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 24, 2008, 9:18am UTC](https://www.ruby-forum.com/t/strange-numeric-validation-problem-in-activerecord/126961/2 "2008-01-24T09:18:44Z")

</div>

What is service\_options in mysql (int, float, etc)?

anyways…

validates\_numericality\_of :replay\_limit, :only\_integer =\> true

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 24, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/strange-numeric-validation-problem-in-activerecord/126961/3 "2008-01-24T17:43:53Z")

</div>

On Jan 24, 2:18 am, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What is service\_options in mysql (int, float, etc)?
> 
> anyways…
> 
> ## validates\_numericality\_of :replay\_limit, :only\_integer =\> true
> 
> Posted viahttp://www.ruby-forum.com/.

The field service\_options is int.

Unfortunately, the validates\_numericality\_of does not work properly.  
It does not detect always  
invalid data. I saw indications of bug reports in this area.

I did find the solution however. I need to use the  
\_before\_type\_cast method. In my case,  
replay\_limit\_before\_type\_cast.  
When I use that method, I can see what the user actually entered.

Thanks,

David
