# Phone number conversions

**URL:** <https://www.ruby-forum.com/t/phone-number-conversions/178456>\
**Category:** Rails\
**Created:** [November 4, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456 "2009-11-04T22:44:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 4, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/1 "2009-11-04T22:44:29Z")

</div>

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:

```
>> @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”

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 4, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/2 "2009-11-04T22:56:46Z")

</div>

On Wed, Nov 4, 2009 at 3:43 PM, Todd A. Jacobs  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @foo.contact.phone.gsub([^\d]+, ‘’)

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

A phone number isn’t a number.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 4, 2009, 11:01pm UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/3 "2009-11-04T23:01:27Z")

</div>

2009/11/4 Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Wed, Nov 4, 2009 at 3:43 PM, Todd A. Jacobs  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 5, 2009, 12:02am UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/4 "2009-11-05T00:02:55Z")

</div>

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

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

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

```
>> 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”

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 4, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/5 "2009-11-04T23:30:00Z")

</div>

Colin L. wrote:

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

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 5, 2009, 12:33am UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/6 "2009-11-05T00:33:21Z")

</div>

On Wed, Nov 4, 2009 at 3:01 PM, Todd A. Jacobs  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 5, 2009, 12:34am UTC](https://www.ruby-forum.com/t/phone-number-conversions/178456/7 "2009-11-05T00:34:19Z")

</div>

Todd A. Jacobs wrote:

> On Wed, Nov 04, 2009 at 09:59:36PM +0000, Colin L. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
