# Combining Two Models into one form

**URL:** <https://www.ruby-forum.com/t/combining-two-models-into-one-form/189443>\
**Category:** Rails\
**Created:** [June 1, 2010, 4:49am UTC](https://www.ruby-forum.com/t/combining-two-models-into-one-form/189443 "2010-06-01T04:49:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matsko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@matsko](https://www.ruby-forum.com/u/matsko)\
**Post date:** [June 1, 2010, 4:49am UTC](https://www.ruby-forum.com/t/combining-two-models-into-one-form/189443/1 "2010-06-01T04:49:32Z")

</div>

Hey Guys.

I’m very new with rails and I’ve been building a CMS application  
backend.

All is going well, but I would like to know if this is possible?

Basically I have two models:

@page { id, name, number }

@extended\_page { id, page\_id, description, image }

The idea is that there are bunch of pages but NOT ALL pages have  
extended\_content. In the event that there is a page with extended  
content then I want to be able to have a form that allows for editing  
both of them.

In the controller:

@page = Page.find(params[:id])  
@extended= Extended.find(:first, :conditions =\> [“page\_id =  
?”,@page.id])  
@combined = … #merge the two somehow

So in the view:

\<%- form\_for @combined do |f| %\>

\<%= f.label :name %\>  
\<%= f.text\_field :name %\>

…

\<%= f.label :description %\>  
\<%= f.text\_field :description %\>

\<% end \>

This way in the controller, there only has to be one model that will be  
updated (which will update to both).

Is this possible?

---

<div class="post-metadata">

**Author:** ![matsko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@matsko](https://www.ruby-forum.com/u/matsko)\
**Post date:** [June 1, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/combining-two-models-into-one-form/189443/2 "2010-06-01T16:00:10Z")

</div>

On Tue, Jun 1, 2010 at 4:49 AM, Matias N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @extended= Extended.find(:first, :conditions =\> ["page\_id =  
> …
> 
> \<%= f.label :description %\>  
> \<%= f.text\_field :description %\>
> 
> \<% end \>
> 
> This way in the controller, there only has to be one model that will be  
> updated (which will update to both).
> 
> Is this possible?

Yes it is !

First of all, you should use associations so that one Page has\_many  
:extendeds (this is where you see that you should find a better name  
!).

Then, you should check out this railscast:

> **[\#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.

It explains exactly what you are trying to achieve.

–  
Gael Muller

---

<div class="post-metadata">

**Author:** ![matsko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@matsko](https://www.ruby-forum.com/u/matsko)\
**Post date:** [June 2, 2010, 8:03pm UTC](https://www.ruby-forum.com/t/combining-two-models-into-one-form/189443/3 "2010-06-02T20:03:38Z")

</div>

Awesome! Thank You! 🙂
