# Updating timestamp when adding model via association

**URL:** <https://www.ruby-forum.com/t/updating-timestamp-when-adding-model-via-association/149017>\
**Category:** Rails\
**Created:** [September 19, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/updating-timestamp-when-adding-model-via-association/149017 "2008-09-19T12:02:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90db22/32.png) [@kenny](https://www.ruby-forum.com/u/kenny)\
**Post date:** [September 19, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/updating-timestamp-when-adding-model-via-association/149017/1 "2008-09-19T12:02:51Z")

</div>

What’s the proper way to update a models timestamp when you add an  
object to one of it’s associations?

For example, if I have a model, Business, that has\_many :addresses,  
when I add a new Address, I’d like the updated\_at timestamp for the  
business object to get updated.

Doing:

@business.addresses.create(…)

Does not update the updated\_at timestamp for @business. What’s the  
best way to get it to update?

Thank you,  
Kenny

---

<div class="post-metadata">

**Author:** ![kenny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90db22/32.png) [@kenny](https://www.ruby-forum.com/u/kenny)\
**Post date:** [September 19, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/updating-timestamp-when-adding-model-via-association/149017/2 "2008-09-19T15:08:33Z")

</div>

On Sep 19, 5:11 pm, Kenny [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does not update the updated\_at timestamp for @business. What’s the  
> best way to get it to update?
> 
> Thank you,  
> Kenny

If you’re using Rails 2.1 and have ActiveRecord::Base.partial\_updates  
= true, you can try this:

class Address  
belongs\_to :business  
before\_save :update\_business  
private  
def update\_business  
if self.business && self.business\_id\_changed?  
self.business.updated\_at = Time.now  
end  
end
