# Form with saved and new model instances

**URL:** <https://www.ruby-forum.com/t/form-with-saved-and-new-model-instances/112950>\
**Category:** Rails\
**Created:** [August 24, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/form-with-saved-and-new-model-instances/112950 "2007-08-24T22:04:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jameskelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jameskelly](https://www.ruby-forum.com/u/jameskelly)\
**Post date:** [August 24, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/form-with-saved-and-new-model-instances/112950/1 "2007-08-24T22:04:05Z")

</div>

Hello all - I’m looking to create a form that would allow the editing  
of existing model instances while also allowing new model instances to  
be created. (its an order form where the model instances would be an  
order line item joining the order to an item)  
It looks like either one alone is easy, because in the former case the  
field names would be foo[id] where the id is from the database, and in  
the latter case, where the models are all new, it would be foo[index].  
I’d like to add the new “empty” rows using some javascript which  
should be easy, I just don’t know how rails will approach the unsaved  
rows in this case.  
How does one mix these models? In my case, I can’t create new “empty”  
models for the new rows, because of some not null constraints in the  
database.  
How do other folks deal with situations like this?

Thanks for any advice or comments  
-James

---

<div class="post-metadata">

**Author:** ![jameskelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jameskelly](https://www.ruby-forum.com/u/jameskelly)\
**Post date:** [August 25, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/form-with-saved-and-new-model-instances/112950/2 "2007-08-25T22:05:13Z")

</div>

> How does one mix these models? In my case, I can’t create new “empty”  
> models for the new rows, because of some not null constraints in the  
> database.

Actually you can create a new “dummy” Model instance and pass it to the  
view. Because it has a bunch of blank fields in it, you cannot save it,  
but it can be used to create the form in the view. The "dummy object  
will disappear as soon as the view is finished rendering never having  
seen the database.

When the user gets done filling in the form, they will hopefully have  
provided you with all the data you need to create another new instance  
of your model. This time you will be able to save it to the database  
(or complain to the user about missing data)

The real trick here is distinguishing between a Form that is returning a  
new object and a form returning an updated existing object. There are a  
number of ways to do this and I don’t like any of them a lot so if you  
need help with this piece, asks someone with a stronger opinion.
