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 Hoepfner 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsWhat 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 Margolis blog.mattmargolis.net
>> >> 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... > 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.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
Timo Hoepfner wrote:>>> >>> 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... >> 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. > > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsYeah 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 Margolis blog.mattmargolis.net
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. On 7/17/06, Matthew Margolis <mrmargolis@wisc.edu> wrote:> > Timo Hoepfner wrote: > >>> > >>> 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... > >> 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. > > > > 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 > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > 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 Margolis > blog.mattmargolis.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060717/7fddc294/attachment-0001.html
Juan Felipe Garcia
2006-Jul-17 20:57 UTC
[Rails] Re: Keeping attributes of two models in sync
Timo Hoepfner 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, > > TimoYou 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 -- Posted via http://www.ruby-forum.com/.