# Attribute Accessors for Phone Number in Form

**URL:** <https://www.ruby-forum.com/t/attribute-accessors-for-phone-number-in-form/169352>\
**Category:** Rails\
**Created:** [June 2, 2009, 7:00pm UTC](https://www.ruby-forum.com/t/attribute-accessors-for-phone-number-in-form/169352 "2009-06-02T19:00:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 2, 2009, 7:00pm UTC](https://www.ruby-forum.com/t/attribute-accessors-for-phone-number-in-form/169352/1 "2009-06-02T19:00:19Z")

</div>

hello,

I have a field in the DB called phone\_number , string 10 chars. In  
my form i want to make these 3 text boxes (phone1, phone2, phone3). I  
am trying to figure out how to assemble this to create the phone  
number before saving. I am trying the following:

In the model

before\_save :make\_phone

def phone1  
end

def phone2  
end

def phone3  
end

def phone1=(p1)  
end

def phone2=(p2)  
end

def phone3=(p3)  
end

def make\_phone  
self.phone\_number = phone1 + phone2 + phon3  
end

however i dont seem to have access to params[:model][:phone1] from  
here. Any ideas on how to make this work ?

Thanks

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 2, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/attribute-accessors-for-phone-number-in-form/169352/2 "2009-06-02T19:31:22Z")

</div>

On Tue, Jun 2, 2009 at 9:59 AM, AD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> before\_save :make\_phone  
> def phone1=(p1)  
> end
> 
> however i dont seem to have access to params[:model][:phone1] from  
> here. Any ideas on how to make this work ?
> 
> Thanks

Hi, what does your param hash look like when you submit the form?

-Conrad

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 2, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/attribute-accessors-for-phone-number-in-form/169352/3 "2009-06-02T19:46:41Z")

</div>

On Jun 2, 5:59 pm, AD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> however i dont seem to have access to params[:model][:phone1] from  
> here. Any ideas on how to make this work ?

Your attribute writer methods don’t do anything at the moment. They’ll  
have to set instance variables which can be accessed by the  
before\_save method. The attr\_accessor shortcut can provide these.

You’re looking for something like:

before\_save :make\_phone  
attr\_accessor :phone\_1, :phone\_2, :phone\_3

def make\_phone  
self.phone\_number = @phone\_1 + @phone\_2 + @phone\_3  
end

-Matt

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 2, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/attribute-accessors-for-phone-number-in-form/169352/4 "2009-06-02T23:29:05Z")

</div>

Matthew MacLeod wrote:  
[…]

> You’re looking for something like:
> 
> before\_save :make\_phone  
> attr\_accessor :phone\_1, :phone\_2, :phone\_3
> 
> def make\_phone  
> self.phone\_number = @phone\_1 + @phone\_2 + @phone\_3  
> end
> 
> -Matt

And you may be able to streamline this a bit with composed\_of.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
