I have an object (call it an ''emitter''), that has_many ''emissions'' objects. Each time I save an emission for a certain emitter, I need to do work on the entire set of emissions for that emitter, including the new emission. I do this work in the emissions after_save handler. My problem arises because, per the rails documentation, the newly saved emission is not actually saved until after the after_save returns. Trying to be clever, before doing anyhting else in the after_save handler, I just pushed the newly saved emission onto the list of emissions in memory: emitter.emissions << self So - that works just great, as long as I never reload the emitter''s emissions (emitter.reload). If I do, then my new emission is gone. Is there any way to force the new emission to really save in the database before the after_save handler completes? Thanks, Yoram -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Could you use an Observer instead? Or perhaps do the reverse and have the after_save of the emission call update_totals with its emitter? Cheers Luke -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thansk Luke. I''m not familiar with observers but will research them. Not yet knowing exactly what they are, I liked the fact that the after_save handler guarantees that all the after_save processing will be completed in the same thread as the save itself, thus avoiding the potential for any race conditions in a multi-threaded environment. As for your second suggestion - if I understand it correctly, that is in fact what I am doing. The emission''s after save handler calls emitter.update_emitter_emissions, which then runs through the entire list of emissions, making changes as necessary. But that doesn''t eliminate the problem... Or did I misunderstand your suggestion here? On Mar 22, 8:53 am, Luke Pearce <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Could you use an Observer instead? Or perhaps do the reverse and have > the after_save of the emission call update_totals with its emitter? > > Cheers > Luke > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yoram Bernet wrote:> As for your second suggestion - if I understand it correctly, that is > in fact what I am doing. The emission''s after save handler calls > emitter.update_emitter_emissions, which then runs through the entire > list of emissions, making changes as necessary. But that doesn''t > eliminate the problem... Or did I misunderstand your suggestion here?Yep, you are correct, sorry my mistake. I''m off out now, but try looking at observers - will have a think about this when I get back. Cheers Luke -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.