Christos Zisopoulos
2008-May-09 12:28 UTC
Observers and notification order for associated records; bug or design?
[I asked this in rails-talk a few days ago, but I didn''t get an answer. Thinking that the question might be a bit more esoteric, and more appropriate to ask in Core. Apologies if I am off-topic] Is it by design that the following happens (Rails 2.0.2)? @post = Post.find(params[:id]) @post.comments.build(params[:comment]) @post.save Assuming I have a PostObserver which observes after_update, what happens is: 1. Transaction BEGINS 2. @post is saved 3. PostObserver#after_update is called 4. Comment is created 5. Transaction COMMITED As this is a transaction I would expect to get the after_update, AFTER all associated records are created/updated but this is not the case. -christos --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Rodrigo Urubatan Ferreira Jardim
2008-May-09 23:11 UTC
Re: Observers and notification order for associated records; bug or design?
The thing is that in rails, the observers and callback methods are called after the object is updated, but they do not consider assotiated objects update as object update. if you modify the post in the comment creation then it would be a Post update and the observer would be called ... On Sex, 2008-05-09 at 05:28 -0700, Christos Zisopoulos wrote:> [I asked this in rails-talk a few days ago, but I didn''t get an > answer. Thinking that the question might be a bit more esoteric, and > more appropriate to ask in Core. Apologies if I am off-topic] > > Is it by design that the following happens (Rails 2.0.2)? > > @post = Post.find(params[:id]) > @post.comments.build(params[:comment]) > @post.save > > Assuming I have a PostObserver which observes after_update, what > happens is: > > 1. Transaction BEGINS > 2. @post is saved > 3. PostObserver#after_update is called > 4. Comment is created > 5. Transaction COMMITED > > As this is a transaction I would expect to get the after_update, > AFTER > all associated records are created/updated but this is not the case. > > -christos > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---