# Help with nested model forms

**URL:** <https://discuss.rubyonrails.org/t/help-with-nested-model-forms/48651>\
**Category:** rubyonrails-talk\
**Created:** [January 18, 2010, 8:09am UTC](https://discuss.rubyonrails.org/t/help-with-nested-model-forms/48651 "2010-01-18T08:09:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://avatars.discourse-cdn.com/v4/letter/e/ed8c4c/32.png) [@elioncho](https://discuss.rubyonrails.org/u/elioncho)\
**Post date:** [January 18, 2010, 8:09am UTC](https://discuss.rubyonrails.org/t/help-with-nested-model-forms/48651/1 "2010-01-18T08:09:31Z")

</div>

class Message \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :author &nbsp;&nbsp;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:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 18, 2010, 11:22am UTC](https://discuss.rubyonrails.org/t/help-with-nested-model-forms/48651/2 "2010-01-18T11: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 &nbsp;&nbsp;has\_many :messages &nbsp;&nbsp;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:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [January 18, 2010, 12:35pm UTC](https://discuss.rubyonrails.org/t/help-with-nested-model-forms/48651/3 "2010-01-18T12:35:46Z")

</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

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

[http://railscasts.com/episodes/196-nested-model-form-part-1](http://railscasts.com/episodes/196-nested-model-form-part-1)

[http://railscasts.com/episodes/197-nested-model-form-part-2](http://railscasts.com/episodes/197-nested-model-form-part-2)

Good luck,

-Conrad
