Todd A. Jacobs wrote:
> On Wed, Nov 04, 2009 at 09:59:36PM +0000, Colin Law wrote:
> 
>> What is wrong with storing it as a string anyway?
> 
> Strings are less efficient than integers, from a database point of view.

Premature optimization. -- and phone numbers really aren't numbers 
anyway.

> More importantly, though, if you store a number as varchar or text, you
> have a much harder time regularizing the output. For example, you
> couldn't do this:
> 
>    >>  foo = ActionView::Base.new
>    >> foo.number_to_phone 1234567890, :area_code => true
>    => "(123) 456-7890"

Of course you can.  It's trivial to do that with a phone number stored 
as a string.  In fact, doing it with an integer would involve converting 
the integer to a string first.

> 
> with a random string. If you try, you just get back the original string,
> like so:
> 
>     >> foo.number_to_phone "123/456.7890x55", :area_code => true
>     => "123/456.7890x55"

Then use to_i or write your own helper.  Better yet, create a 
PhoneNumber class and give it a meaningful to_s method.

> 
> The whole idea here is to regularize the data IN THE DATABASE, so that
> the output can be customized (and perhaps even changed later) without
> having to change the schema. Isn't there some way to strip out the
> characters and extra digits *after* form submission but *before* the
> assignment to the model object?

Yup.  You can use an ActiveRecord callback, or (if you create a 
PhoneNumber class) use composed_of and put this logic in the 
constructor.

> 
> --
> "Oh, look: rocks!"
>   -- Doctor Who, "Destiny of the Daleks"

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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