# Safely override an association accessor

**URL:** <https://www.ruby-forum.com/t/safely-override-an-association-accessor/144955>\
**Category:** Rails\
**Created:** [August 4, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/safely-override-an-association-accessor/144955 "2008-08-04T18:19:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [August 4, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/safely-override-an-association-accessor/144955/1 "2008-08-04T18:19:39Z")

</div>

At the moment, when I print say an invoice, I access  
@order.customer.address.

Now I need to enhance the app to introduce the ability to map in  
customer\_detail from a new CustomerDetail model if one exists for that  
order. But I want to avoid any changes to the existing code base

I have created TelephoneOrder \< Order which carries all the new stuff  
for Orders so as to minimise changes to existing stuff…

Now Rails is clever because when I find Order, it knows if  
type=‘TelephoneOrder’ and instantiates as TelephoneOrder class.

I am thinking therefore that perhaps I can do something in  
TelephoneOrder to ‘intercept’ @order.customer, and  
@order.customer.address etc.

I have tried def address in TelephoneOrder and this does indeed  
override the normal customer method only if the order is type  
TelephoneOrder - Great so far!

This is where I am stuck. For a Telephone Order, I should now be able  
to do something to replace the normal fields from Customer with the  
fields from CustomerDetail.

My first thought was simply to use the customer object for that order  
but change the fields as required before returning it. This ought to  
fix all of the display occurrences, but would be dangerous in case the  
customer gets saved somewhere, the fields will get overwritten.

So my question is - is there a way that I can get the customer object,  
but return an inherited object, or something, which would effectively  
be read only?

Tonypm
