# Mapping active record objects to virtual attributes

**URL:** <https://www.ruby-forum.com/t/mapping-active-record-objects-to-virtual-attributes/127739>\
**Category:** Rails\
**Created:** [January 31, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/mapping-active-record-objects-to-virtual-attributes/127739 "2008-01-31T21:11:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [January 31, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/mapping-active-record-objects-to-virtual-attributes/127739/1 "2008-01-31T21:11:30Z")

</div>

I’ve got a phone number field defined in a model as a single String.  
However, the form used to input the phone number exposes the number as  
three distinct fields to help the user enter the number in the correct  
format. I’m mapping the three input parameters to the single string  
in a before\_validation method which seems to work fine, but I want to  
be able to map the single string to the three distinct fields when the  
record is loaded from the database. What’s the correct hook to do  
that? I tried to provide a phone\_number= method that used  
self[:phone\_number]=… to assign the attribute and then split the  
phone number up into the three fields to be returned by accessors, but  
AR doesn’t seem to call this method when the record is loaded from the  
database. What’s the best way to accomplish this?

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 1, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/mapping-active-record-objects-to-virtual-attributes/127739/2 "2008-02-01T23:11:31Z")

</div>

On Jan 31, 2008 3:10 PM, bigbanger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> phone number up into the three fields to be returned by accessors, but  
> AR doesn’t seem to call this method when the record is loaded from the  
> database. What’s the best way to accomplish this?

class MyModel \< ActiveRecord::Base

…

def after\_find  
# call your method to split the phone number here  
end

Another way to do this is with a virtual accessors

def area\_code  
phone\_number[0…2]  
end

def area\_code=(area\_code)  
raise ArgumentError.new(‘invalid area code’) unless  
area\_code.length == 3  
phone\_number[0…2] = area\_code  
end

of course this will differ depending just how you represent the numbers.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [July 3, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/mapping-active-record-objects-to-virtual-attributes/127739/3 "2008-07-03T17:33:49Z")

</div>

Rick Denatale wrote:

> On Jan 31, 2008 3:10 PM, bigbanger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > phone number up into the three fields to be returned by accessors, but  
> > AR doesn’t seem to call this method when the record is loaded from the  
> > database. What’s the best way to accomplish this?

I figured out how to use Aggregations to provide the necessary piecing  
and reassembly of phone number  
[http://blogs.entertonement.com/nerdery/2008/07/rails-multipara.html](http://blogs.entertonement.com/nerdery/2008/07/rails-multipara.html)
