# Help with nested model forms

**URL:** <https://www.ruby-forum.com/t/help-with-nested-model-forms/181789>\
**Category:** Rails\
**Created:** [January 18, 2010, 9:10am UTC](https://www.ruby-forum.com/t/help-with-nested-model-forms/181789 "2010-01-18T09:10:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 18, 2010, 9:10am UTC](https://www.ruby-forum.com/t/help-with-nested-model-forms/181789/1 "2010-01-18T09:10:51Z")

</div>

class Message \< ActiveRecord::Base  
belongs\_to :author  
accepts\_nested\_attributes\_for :author  
end

I’m using the above and it’s working perfectly. The message gets saved  
with an author\_id attribute which references the author (tha author  
record gets saved too).

The problem:

Sometimes I don’t want to save the author because the author already  
exists (I just want to reference it). I just want the message to get  
saved with the author\_id reference. How can I override or modify the  
normal saving behaviour when using the accepts\_nested\_attributes\_for  
method?

Thanks,

ElÃ­as

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 18, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/help-with-nested-model-forms/181789/2 "2010-01-18T12:22:04Z")

</div>

Hello Elias

I wonder that this works in any way. Normally in an 1:n relation the 1  
accepts the nested attributes for the n. So this would be the way to go  
for you:

class Author \< ActiveRecord::Base  
has\_many :messages  
accepts\_nested\_attributes\_for :messages  
end

Then you can make a (possibly almost empty) form for Author with  
fields\_for the message(s).

Regards, T.

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 18, 2010, 1:37pm UTC](https://www.ruby-forum.com/t/help-with-nested-model-forms/181789/3 "2010-01-18T13:37:09Z")

</div>

On Mon, Jan 18, 2010 at 12:09 AM, elioncho [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

ElÃ­as, I would recommend taking a look at the following screencasts:

> **[\#196 Nested Model Form Part 1 - RailsCasts](http://railscasts.com/episodes/196-nested-model-form-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#197 Nested Model Form Part 2 - RailsCasts](http://railscasts.com/episodes/197-nested-model-form-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Good luck,

-Conrad
