# Problems with after\_save callback

**URL:** <https://www.ruby-forum.com/t/problems-with-after-save-callback/185545>\
**Category:** Rails\
**Created:** [March 22, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/problems-with-after-save-callback/185545 "2010-03-22T15:54:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e0b2c6/32.png) [@yoram](https://www.ruby-forum.com/u/yoram)\
**Post date:** [March 22, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/problems-with-after-save-callback/185545/1 "2010-03-22T15:54:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yoram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e0b2c6/32.png) [@yoram](https://www.ruby-forum.com/u/yoram)\
**Post date:** [March 22, 2010, 4:53pm UTC](https://www.ruby-forum.com/t/problems-with-after-save-callback/185545/2 "2010-03-22T16:53:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yoram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e0b2c6/32.png) [@yoram](https://www.ruby-forum.com/u/yoram)\
**Post date:** [March 22, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/problems-with-after-save-callback/185545/3 "2010-03-22T17:03:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![yoram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e0b2c6/32.png) [@yoram](https://www.ruby-forum.com/u/yoram)\
**Post date:** [March 22, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/problems-with-after-save-callback/185545/4 "2010-03-22T17:16:51Z")

</div>

Yoram B. 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
