# After\_save question

**URL:** <https://www.ruby-forum.com/t/after-save-question/109573>\
**Category:** Rails\
**Created:** [July 16, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/after-save-question/109573 "2007-07-16T23:52:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [July 16, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/after-save-question/109573/1 "2007-07-16T23:52:22Z")

</div>

Hi all,

In my model class I need to update an attribute with the ID of the  
record  
right after the model object has been saved.  
I intended to use the after\_save callback for this purpose.

I don’t know how to update this automatically after saving an object.  
Calling save in the after\_save hook, causes an infinite loop.  
Write\_attribute does not save to the Database. What can I do, to make  
this  
happen auto-magically?

class Product \< ActiveRecord::Base

after\_save :update\_lineage

def update\_lineage  
write\_attribute(:lineage, “#{id},”) # this does not save the  
attribute  
to the db  
# save! # this does save, but calls the after\_save callback again,  
causing a infinite loop  
end

end

Thanks,

Harm de Laat

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [July 17, 2007, 12:07am UTC](https://www.ruby-forum.com/t/after-save-question/109573/2 "2007-07-17T00:07:23Z")

</div>

Hi Harm,

> class Product \< ActiveRecord::Base

-after\_save :update\_lineage  
+after\_create :update\_lineage

> def update\_lineage

- 

```
 write_attribute(:lineage, "#{id},") # this does not save the

```

- attribute to the db
- 

```
 # save! # this does save, but calls the after_save callback 

```

again,

- causing a infinite loop

- update\_attribute :lineage, “#{id},”

> end
> 
> end

Regards  
Florian
