HighDots Forums  

Phone number conversions

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss Phone number conversions in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Todd A. Jacobs
 
Posts: n/a

Default Phone number conversions - 11-04-2009 , 04:43 PM






I'm trying to figure out a good way to store phone numbers as an integer
in a rails app without having to have separate columns in the table for
each part of the phone number. I want people to enter the phone number
in a single entry field, but to be able to store it as an integer rather
than as a string.

My idea was to allow free-form text entry in the form, but to add
something into create() to turn the string into an integer. From the
console, though:

Quote:
@foo=User.new
@foo.build_contact
@foo.contact.phone='123-456-7890'
p @foo.contact.phone
=> 123

Obviously, rails is converting the string to an integer before saving it
to the database, but this doesn't get me where I need to go. My idea was
to simply add some evaluation in create like:

@foo.contact.phone.gsub([^\d]+, '')

but this obviously isn't going to work. So, if my model defines
contact.phone as an integer, how can I coerce numbers entered from the
new/edit views into the appropriate data type?

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


--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #2  
Old   
Greg Donald
 
Posts: n/a

Default Re: Phone number conversions - 11-04-2009 , 04:55 PM






On Wed, Nov 4, 2009 at 3:43 PM, Todd A. Jacobs
<tjacobs-sndr-b4faac (AT) codegnome (DOT) org> wrote:
Quote:
* *@foo.contact.phone.gsub([^\d]+, '')
What about storing things like 123-123-1234 ext 123 ?

A phone number isn't a number.


--
Greg Donald
http://destiney.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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #3  
Old   
Colin Law
 
Posts: n/a

Default Re: Phone number conversions - 11-04-2009 , 04:59 PM



2009/11/4 Greg Donald <gdonald (AT) gmail (DOT) com>:
Quote:
On Wed, Nov 4, 2009 at 3:43 PM, Todd A. Jacobs
tjacobs-sndr-b4faac (AT) codegnome (DOT) org> wrote:
Â* Â*@foo.contact.phone.gsub([^\d]+, '')

What about storing things like 123-123-1234 ext 123 ?

A phone number isn't a number.
Also UK phone numbers generally start with 0 so 0123456789 would be
indistinguishable from 123456789

What is wrong with storing it as a string anyway?

Colin

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #4  
Old   
T. N.t.
 
Posts: n/a

Default Re: Phone number conversions - 11-04-2009 , 05:30 PM



Colin Law wrote:
Quote:
What is wrong with storing it as a string anyway?
I also thought about a way of normalizing phone numbers to make a
'reverse' search possible (I mean finding an address by phone number).
But a number type would of course not make that easier. Leading zeros
are important unless you would normalize all phone numbers to full
international numbers that are the same from everywhere in the world.

Interesting subject!

Regards, T.

--
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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #5  
Old   
Todd A. Jacobs
 
Posts: n/a

Default Re: Phone number conversions - 11-04-2009 , 06:01 PM



On Wed, Nov 04, 2009 at 09:59:36PM +0000, Colin Law wrote:

Quote:
What is wrong with storing it as a string anyway?
Strings are less efficient than integers, from a database point of view.
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:

Quote:
foo = ActionView::Base.new
foo.number_to_phone 1234567890, :area_code => true
=> "(123) 456-7890"

with a random string. If you try, you just get back the original string,
like so:

Quote:
foo.number_to_phone "123/456.7890x55", :area_code => true
=> "123/456.7890x55"

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?

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


--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #6  
Old   
Hassan Schroeder
 
Posts: n/a

Default Re: Phone number conversions - 11-04-2009 , 06:31 PM



On Wed, Nov 4, 2009 at 3:01 PM, Todd A. Jacobs
<tjacobs-sndr-b4faac (AT) codegnome (DOT) org> wrote:

Quote:
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"
which you can only do with US phone numbers, without extensions.
That seems pretty limited.[*]

And if you're going to try to not parse the data on the way into the
database, how are you going to store your "123/456.7890x55"
example?

Stored strictly as an integer, foo.number_to_phone will return
12(345) 678-9055 -- is that what you want?
[*] BTW, it's not that long ago that my wife ran a small island resort
in the Kingdom of Tonga, and the resort phone # was '5' :-)

FWIW,
--
Hassan Schroeder ------------------------ hassan.schroeder (AT) gmail (DOT) com
twitter: @hassan

--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #7  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Phone number conversions - 11-04-2009 , 06:34 PM



Todd A. Jacobs wrote:
Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
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 rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.