# 2.3 Nested Model Forms; build new without updating existing

**URL:** <https://www.ruby-forum.com/t/2-3-nested-model-forms-build-new-without-updating-existing/167768>\
**Category:** Rails\
**Created:** [May 12, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/2-3-nested-model-forms-build-new-without-updating-existing/167768 "2009-05-12T12:07:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guitaronin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Guitaronin](https://www.ruby-forum.com/u/Guitaronin)\
**Post date:** [May 12, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/2-3-nested-model-forms-build-new-without-updating-existing/167768/1 "2009-05-12T12:07:33Z")

</div>

I’m trying to build something similar to a Facebook messaging app, in  
which the view displays all the messages for a particular thread, with  
a form box for replying at the bottom of the thread.

The problem is that using fields\_for generates form fields for each  
existing record, and for one new record. In this case, there is no  
need to edit existing messages - just a need to add new messages.

Does anybody know how to do this? Code is below.

Models:  
class MessageThread \< ActiveRecord::Base  
has\_many :messages  
accepts\_nested\_attributes\_for :messages, :allow\_destroy =\> true  
end

class Message \< ActiveRecord::Base  
belongs\_to :message\_threads  
end

View (show.html.erb)

\<% @message\_thread.messages.each do |message| %\>  
\<%= message.message %\>  
\<% end %\>

\<% form\_for @message\_thread do |thread\_form| -%\>  
\<% thread\_form.fields\_for :messages do |message| %\>  
\<%= message.label :message %\>  
\<%= message.text\_field :message %\>  
\<% end %\>

\<%= submit\_tag ‘Send Message’ %\>  
\<% end -%\>

Thanks.

---

<div class="post-metadata">

**Author:** ![Guitaronin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Guitaronin](https://www.ruby-forum.com/u/Guitaronin)\
**Post date:** [May 14, 2009, 10:36pm UTC](https://www.ruby-forum.com/t/2-3-nested-model-forms-build-new-without-updating-existing/167768/2 "2009-05-14T22:36:57Z")

</div>

I just wanted to follow up with my simple (maybe obvious) solution…

> \<% form\_for @message\_thread do |thread\_form| -%\>  
> \<% thread\_form.fields\_for :messages do |message| %\>  
> \<% if message.object.new\_record? %\> # The magic!  
> \<%= message.label :message %\>  
> \<%= message.text\_field :message %\>  
> \<% end %\>  
> \<% end %\>

Duh! If there’s a smarter way, I’m still listening.

---

<div class="post-metadata">

**Author:** ![Guitaronin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Guitaronin](https://www.ruby-forum.com/u/Guitaronin)\
**Post date:** [May 19, 2009, 12:49am UTC](https://www.ruby-forum.com/t/2-3-nested-model-forms-build-new-without-updating-existing/167768/3 "2009-05-19T00:49:59Z")

</div>

Hi,

On May 12, 9:43 am, Guitaronin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The problem is that using fields\_for generates form fields for each  
> existing record, and for one new record.

The ‘new record’ is behaviour I’m unaware of! I think that you have to  
build the new record in the association.

> end  
> \<%= message.text\_field :message %\>  
> \<% end %\>

Nested attributes fields\_for supports setting an explicit object as  
the second parameter, like so:  
\<% thread\_form.fields\_for(:messages, Message.new) do |message| %\>

Although the proper way would be to set @new\_message or similar in the  
controller and pass that, or thread\_form.object.messages.build().

Hope that helps,  
Andrew
