# Multiple Models in one form

**URL:** <https://www.ruby-forum.com/t/multiple-models-in-one-form/188227>\
**Category:** Rails\
**Created:** [May 8, 2010, 1:53am UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/188227 "2010-05-08T01:53:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [May 8, 2010, 1:53am UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/188227/1 "2010-05-08T01:53:53Z")

</div>

I have had the worst time getting my forms to accept input for more  
than one model. I tried using accepts\_nested, and other techniques  
and finally got it all working. But it was not fun. I never did get  
accepts\_nested working!

I do need more experience.

I did hit upon one idea that I just wanted to run by the community.  
Let me know what you think.

If you have one form that will need to send values to several  
models… how about creating a “Form” model with generic attributes.  
This “Form” model could for example have fields integer1:integer,  
integer2:integer … etc… and have fields string1:string,  
string2:string… etc…

You would build a “Form” model that could handle any form in your  
site.

Then you just collect the values into this “Form” model… after they  
are in you just assign them to all your other models. No need to go  
crazy with all those parameters[][:this][:that].

It for sure may not be the MOST efficient way to deal with the  
database, but it can’t be that bad.

Thoughts?

Shawn

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [May 8, 2010, 10:58pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/188227/2 "2010-05-08T22:58:03Z")

</div>

“slindsey3000” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote  
in message  
news:removed\_email\_address@domain.invalid…

> I have had the worst time getting my forms to accept input for more  
> than one model. I tried using accepts\_nested, and other techniques  
> and finally got it all working. But it was not fun. I never did get  
> accepts\_nested working!

To help you get accepts\_nested working, since that is so much easier  
than  
the alternatives if you can get it working, please watch the following  
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.

If you have a strong aversion to watching a screencast, you may instead  
substitue the asciicast:  
[http://asciicasts.com/episodes/196-nested-model-form-part-1](http://asciicasts.com/episodes/196-nested-model-form-part-1)

That should be enough to get accepts\_nested\_attributes\_for working, at  
least  
in the way it was intended to work.

If you have further questions after watching that, please let us know.

The next railscast/asciicast covers a bit more of the topic, but is  
probably  
not relevant for your purposes, as it sounds like you plan on having  
only  
say a single subform per child model.

I hope that helps. If not please try following along with it, so you get  
confortable with how it is used in that case, and then ask any more  
specific  
questions.

Perhaps you want to post attributes for a parent model to a child model.  
That is tricker, and something we could help you with.

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [May 9, 2010, 1:03am UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/188227/3 "2010-05-09T01:03:51Z")

</div>

slindsey3000 wrote:

> I have had the worst time getting my forms to accept input for more  
> than one model. I tried using accepts\_nested, and other techniques  
> and finally got it all working. But it was not fun. I never did get  
> accepts\_nested working!

accepts\_nested works very easily. If you can tell us what problems  
you’re having, I’m sure we can help.

> I do need more experience.
> 
> I did hit upon one idea that I just wanted to run by the community.  
> Let me know what you think.
> 
> If you have one form that will need to send values to several  
> models… how about creating a “Form” model with generic attributes.  
> This “Form” model could for example have fields integer1:integer,  
> integer2:integer … etc… and have fields string1:string,  
> string2:string… etc…
> 
> You would build a “Form” model that could handle any form in your  
> site.
> 
> Then you just collect the values into this “Form” model… after they  
> are in you just assign them to all your other models. No need to go  
> crazy with all those parameters[][:this][:that].

Bad idea. Beside the conceptual wrongness, the resulting params hash  
will be _harder_ to work with (because the names won’t be descriptive),  
not easier. If you want to do soething like this properly, check out  
the Presenter pattern.

> It for sure may not be the MOST efficient way to deal with the  
> database, but it can’t be that bad.

It is that bad. Don’t even bother.

> Thoughts?

Learn the framework, don’t fight it and kludge your way around it.

> Shawn

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [May 24, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/188227/4 "2010-05-24T19:05:34Z")

</div>

Thanks all!

---

<div class="post-metadata">

**Author:** ![slindsey3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@slindsey3000](https://www.ruby-forum.com/u/slindsey3000)\
**Post date:** [May 8, 2010, 10:19pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/188227/5 "2010-05-08T22:19:25Z")

</div>

Shawn,

If you’re doing something in Rails and it’s “not fun” then chances are  
you can be doing it a better way!

Using accepts\_nested with appropriate relationship declarations will  
eliminate going “crazy with all those parameters[][:this][:that]”.

I highly recommend you work with accepts\_nested. There are plenty of  
resources out there to help you along the way. It will bring back the  
fun.

Matt
