On Nov 16, 2009, at 8:47 AM, Colin Law wrote:

>
> 2009/11/16 Todd A. Jacobs <[email protected]>:
>>
>> On Mon, Nov 16, 2009 at 11:38:14AM +0000, Colin Law wrote:
>>
>>> Is it a string or numeric field in the db?
>>
>> It's an integer.
>>
>
> I think that is the problem, as noted earlier validates_length_of does
> not really make much sense for an integer.  How many digits in 0123
> for example?  It has four digits but will be stored in the db as 123
> so will no longer have four digits when you read it back.  Use
> validates_numericality_of, specifying a range, to check it is an
> integer in the required range.  Whether integer is the right field
> type for zipcode is another matter.  If you needed to go international
> you would find that often they are not numbers.
>
> Colin


Depending on how you get the integer out of that string, it might be  
considered octal.

irb> "0123".to_i
=> 123
irb> Integer("0123")
=> 83

As Colin points out, a "ZIP code" is not a number (and a "postal code"  
even less so) as the leading zeros and the USA's ZIP+4 extension.  If  
you stop thinking about the field as a number just because it is  
frequently made up of digits, you'll likely have an easier time of it.

-Rob

Rob Biedenharn          http://agileconsultingllc.com
[email protected]




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to