Keeping attributes of two models in sync

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

Timo H. wrote:

Timo


Rails mailing list
[email protected]
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

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

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.

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

Timo H. wrote:

belongs_to and has_one would be the ids and AR manages those for you.


Rails mailing list
[email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs