# Keeping attributes of two models in sync

**URL:** <https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691>\
**Category:** Rails\
**Created:** [July 17, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691 "2006-07-17T16:27:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thoepfner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@thoepfner](https://www.ruby-forum.com/u/thoepfner)\
**Post date:** [July 17, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691/1 "2006-07-17T16:27:31Z")

</div>

Hi,

I have a Product and a corresponding Keyword (1:1 via belongs\_to and  
has\_one). I want to ensure, that two attributes (:name) of the two  
models alway stay in sync. What’s the easiest way to accomplish that?

I did play around with before\_/after\_save hooks and with  
AR::Observers but in the end I’d always get infinite recursion…

Thanks,

Timo

---

<div class="post-metadata">

**Author:** ![thoepfner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@thoepfner](https://www.ruby-forum.com/u/thoepfner)\
**Post date:** [July 17, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691/2 "2006-07-17T16:36:44Z")

</div>

Timo H. wrote:

> Timo
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> What exactly do you mean by ensuring that :name of the two models stay  
> in sync? You should only be storing each models name in one place so  
> this should be a non issue. All you need to worry about for belongs\_to  
> and has\_one would be the ids and AR manages those for you.

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)

---

<div class="post-metadata">

**Author:** ![thoepfner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@thoepfner](https://www.ruby-forum.com/u/thoepfner)\
**Post date:** [July 17, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691/3 "2006-07-17T16:56:14Z")

</div>

> place so this should be a non issue. All you need to worry about  
> for belongs\_to and has\_one would be the ids and AR manages those  
> for you.

I’m using STI for different Keyword types. Some, but not all Keywords  
have a related Product model. In that special case, i want to ensure,  
that the name attribute of the Keyword always has the same value as  
the name attribute of the Product and vice versa. So if someone  
renames a Keyword, the corresponding Product should be renamed and  
the other way around.

Timo

---

<div class="post-metadata">

**Author:** ![thoepfner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@thoepfner](https://www.ruby-forum.com/u/thoepfner)\
**Post date:** [July 17, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691/4 "2006-07-17T17:25:55Z")

</div>

I second Matthew’s idea… make your own separate save / update methods  
for  
this reason. You could actually lear a lot by overriding the save() and  
update\_attribute() methods on both of your models. It would be a fun  
experience.

---

<div class="post-metadata">

**Author:** ![thoepfner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@thoepfner](https://www.ruby-forum.com/u/thoepfner)\
**Post date:** [July 17, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691/5 "2006-07-17T22:57:25Z")

</div>

Timo H. wrote:

> Hi,
> 
> I have a Product and a corresponding Keyword (1:1 via belongs\_to and  
> has\_one). I want to ensure, that two attributes (:name) of the two  
> models alway stay in sync. What’s the easiest way to accomplish that?
> 
> I did play around with before\_/after\_save hooks and with  
> AR::Observers but in the end I’d always get infinite recursion…
> 
> Thanks,
> 
> Timo

You can always normalize and drop one of the two name columns if they  
are always going to be the same. Keep it in the product and you can get  
keywordobject.product.name instead of keywordobject.name

---

<div class="post-metadata">

**Author:** ![thoepfner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@thoepfner](https://www.ruby-forum.com/u/thoepfner)\
**Post date:** [July 17, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/keeping-attributes-of-two-models-in-sync/98691/6 "2006-07-17T17:19:01Z")

</div>

Timo H. wrote:

> > belongs\_to and has\_one would be the ids and AR manages those for you.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> Yeah if you have before\_save for both of them that could get ugly. I  
> would probably write a new method save\_and\_update\_keywords or something  
> like that and call that when appropriate. You would then write a  
> similar method for your Keywords save\_and\_update\_products, and only call  
> it when you are editing keywords.

You could also try getting the current controller in a before\_save  
filter (not sure if that is possible) and only run the before\_save only  
if the current controller deals with the model you are currently  
editing. This would ensure that you don’t hit infinite recursion with  
your before\_saves calling each other forever.

I am sure there is a more elegant solution out there but those are the  
first two ideas that I had.

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)
