# :has\_many and :after\_save

**URL:** <https://www.ruby-forum.com/t/has-many-and-after-save/169005>\
**Category:** Rails\
**Created:** [May 28, 2009, 11:19am UTC](https://www.ruby-forum.com/t/has-many-and-after-save/169005 "2009-05-28T11:19:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [May 28, 2009, 11:19am UTC](https://www.ruby-forum.com/t/has-many-and-after-save/169005/1 "2009-05-28T11:19:37Z")

</div>

Having one to many connection (for example post has many comments), what  
is the most efficient way of saving all comments when post is saved.  
Using after\_save on Post would introduce the n+1 problem. Is there any  
way of saving the whole structure at once?

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [May 28, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/has-many-and-after-save/169005/2 "2009-05-28T12:26:13Z")

</div>

class Post  
has\_one :author, :autosave =\> true  
end

The autosave will take care of saving associations when the parent is  
saved.

On May 28, 2:19 pm, Milan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
