# "Overriding" association build methods

**URL:** <https://www.ruby-forum.com/t/overriding-association-build-methods/176856>\
**Category:** Rails\
**Created:** [October 7, 2009, 5:04am UTC](https://www.ruby-forum.com/t/overriding-association-build-methods/176856 "2009-10-07T05:04:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tristan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tristan](https://www.ruby-forum.com/u/tristan)\
**Post date:** [October 7, 2009, 5:04am UTC](https://www.ruby-forum.com/t/overriding-association-build-methods/176856/1 "2009-10-07T05:04:53Z")

</div>

Hey everyone

I have (in a nutshell) two models: Customer and Order. Customer  
has\_many Orders and Order belongs\_to Customer. The Order model  
contains fields such as name, address and phone that mirror the  
associated Customer’s fields at the time the order was placed. The  
reason for this is that if the customer changes their details then the  
order contains the details that were current when the order was  
placed.

I’m looking for a smart way to automatically set these fields when I  
create a new Order for a Customer. I would like to create an order  
using @order = Customer.orders.build. I could then set each field  
manually or even use .build(attributes) but this won’t be DRY when I  
need to create orders in the same way in other parts of the site.

So, I’m looking for a way to intercept the build method and initialise  
those fields there. Unless someone has another good idea? I had  
thought of using ActiveRecord callbacks to do these but there are some  
fields (namely delivery address fields) that the customer can edit for  
each order but are still initialised from the Customer record.

Thanks  
Tristan

---

<div class="post-metadata">

**Author:** ![tristan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tristan](https://www.ruby-forum.com/u/tristan)\
**Post date:** [October 7, 2009, 9:27am UTC](https://www.ruby-forum.com/t/overriding-association-build-methods/176856/2 "2009-10-07T09:27:08Z")

</div>

On Wed, Oct 7, 2009 at 5:03 AM, Tristan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> each order but are still initialised from the Customer record.

Can’t you do that on the after\_save hook?

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![tristan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tristan](https://www.ruby-forum.com/u/tristan)\
**Post date:** [October 7, 2009, 10:47am UTC](https://www.ruby-forum.com/t/overriding-association-build-methods/176856/3 "2009-10-07T10:47:21Z")

</div>

2009/10/7 Tristan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hey everyone
> 
> I have (in a nutshell) two models: Customer and Order. Customer  
> has\_many Orders and Order belongs\_to Customer. The Order model  
> contains fields such as name, address and phone that mirror the  
> associated Customer’s fields at the time the order was placed. The  
> reason for this is that if the customer changes their details then the  
> order contains the details that were current when the order was  
> placed.

Consider factoring the contact details out into a separate table, with  
Customer and Order each has\_one contact\_info. Then a new order can  
use the customer’s current info but if the customer info changes make  
a new contact\_info record, leaving the old one for the orders.

Colin
