First off, thanks for your help. I am having trouble here completely understanding model relationships. Maybe you can shed some light on this subject. Let''s say I have 2 models: Category Product Where a category has ONE product. Just for the sake of making my point. Then we have 2 objects: category1 product1 product1 belongs to category1. Then I do this: product1.price => 20.00 product1.price = 100.00 => 100.00 product1.category.product.price => 20.00 When visualizing the relationships between these 2 objects wouldn''t it make more sense for that last line of code to return 100.00? Is there a plugin or any kind of configuration that can make this possible? Thanks for your help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Johnson wrote:> product1.price > => 20.00 > product1.price = 100.00 > => 100.00 > product1.category.product.price > => 20.00 > > When visualizing the relationships between these 2 objects wouldn''t it > make more sense for that last line of code to return 100.00?Looks like you''re just missing a save. product1.price #=> 20.00 product1.price = 100.00 #=> 100.00 product1.save #=> true product1.category.product.price #=> 100.00 If that does not work, the product could be cached in the category, so try: product1.category.product.reload.price Dan Manges --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dan Manges wrote:> Ben Johnson wrote: >> product1.price >> => 20.00 >> product1.price = 100.00 >> => 100.00 >> product1.category.product.price >> => 20.00 >> >> When visualizing the relationships between these 2 objects wouldn''t it >> make more sense for that last line of code to return 100.00? > > Looks like you''re just missing a save. > > product1.price #=> 20.00 > product1.price = 100.00 #=> 100.00 > product1.save #=> true > product1.category.product.price #=> 100.00 > > If that does not work, the product could be cached in the category, so > try: > product1.category.product.reload.price > > Dan MangesThanks for the help. The reason I made this post is because I was trying to do that code excerpt in the "validate" method in the model. So I really can''t add that save in there, otherwise I would cause an infinite loop. The problem I''m having is that some models rely on values from other models to be valid. If I change that value I want to make sure all of the other model''s objects are ok with it, but they all see the old value, not the value in the object. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Johnson wrote:> The problem I''m having is that some models rely on values from other > models to be valid. If I change that value I want to make sure all of > the other model''s objects are ok with it, but they all see the old > value, not the value in the object.If you''re going to have models rely on other models to be valid, I would approach it a little differently. If changing the price of a product can make a category invalid, in the product validation I would check for this and then error with something like "price cannot be set to this because categories depend on having a product with a certain price" (or however your relationships are set up). I''ve done this before in an application using workflows. If a certain model is using a workflow, the workflow cannot be set to inactive. So the one model validates that it has an active workflow, but then the workflow model validates that it cannot be set to inactive if other models are using it. Hopefully that makes sense. Dan Manges --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---