# Belongs\_to trigger on update?

**URL:** <https://www.ruby-forum.com/t/belongs-to-trigger-on-update/104326>\
**Category:** Rails\
**Created:** [May 16, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/belongs-to-trigger-on-update/104326 "2007-05-16T20:07:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [May 16, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/belongs-to-trigger-on-update/104326/1 "2007-05-16T20:07:03Z")

</div>

This may be a rails bug, but I can’t figure out how to do attribute  
overrides for the attribute created for a belongs\_to relationship

I have a simple relationship:  
User belongs\_to Domain

User has a property email\_address which I want to update if Domain is  
ever changed

I tried in User

def domain=(new\_domain)  
super  
end

but now the ability to assign new domains is totally broken and no  
longer works (simply nothing happens)…

I suspect that this needs some magic reflection hacking, but can someone  
set me on the right path to figure out how to work around it. Basically  
I want to just intercept the update call and run some custom code to  
update the User model.

Grateful for some ideas

Ed W

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [May 16, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/belongs-to-trigger-on-update/104326/2 "2007-05-16T22:24:18Z")

</div>

Ed W wrote:

> I tried in User  
> I want to just intercept the update call and run some custom code to  
> update the User model.

Yes, you can’t use super because AR defines the domain= method on  
the User class itself.

Instead use:

def domain\_with\_email\_update=(new\_domain)  
self.email\_address = blah(new\_domain)  
self.domain\_without\_email\_update = new\_domain  
end  
alias\_method\_chain :domain=, :email\_update

Now user.domain = … will also do an email update, but you can  
use user.domain\_without\_email\_update = … for cases where you  
don’t want the email address updated.

–  
We develop, watch us RoR, in numbers too big to ignore.
