# Updating multiple records with form

**URL:** <https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515>\
**Category:** Rails\
**Created:** [July 15, 2006, 9:53am UTC](https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515 "2006-07-15T09:53:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geoff\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58f4c7/32.png) [@Geoff\_D](https://www.ruby-forum.com/u/Geoff_D)\
**Post date:** [July 15, 2006, 9:53am UTC](https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515/1 "2006-07-15T09:53:40Z")

</div>

I have an app that has two models – products and details. I’m building  
a form that allows a user to create a new product and specify a  
description, etc.

The form needs to allow for creation of multiple items in the details  
model/table. for example, I have a block of form items that collect  
info for the men’s version of the product – inventory count for each  
size, etc. And a similar block of form items for women’s versions.

I’m wondering how I can specify that each inventory count box creates a  
new detail record with the right values (ie,  
product\_id=123456,gender=mens, size=XL, inventory=20)

---

<div class="post-metadata">

**Author:** ![Geoff\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58f4c7/32.png) [@Geoff\_D](https://www.ruby-forum.com/u/Geoff_D)\
**Post date:** [July 15, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515/2 "2006-07-15T15:34:09Z")

</div>

Geoff D. wrote:

> new detail record with the right values (ie,  
> product\_id=123456,gender=mens, size=XL, inventory=20)

All form helpers have an :index option that will separate  
the posted parameters of each item by a separate hash id.  
Note: Be careful to put the index option for select helper  
calls in the html\_options hash rather than the options hash.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Geoff\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58f4c7/32.png) [@Geoff\_D](https://www.ruby-forum.com/u/Geoff_D)\
**Post date:** [July 16, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515/3 "2006-07-16T18:24:16Z")

</div>

Mark,

Thanks for your suggestion. I’m playing around w/ :index, but still  
having difficulty. I’ve tried about every syntax I can imagine to get  
the correct info into :index, without luck. This is the closest I’ve  
gotten.

product name  
\<%= text\_field 'product', 'name' %\>

product description  
\<%= text\_field 'product', 'description' %\>

price  
\<%= text\_field 'product', 'price' %\>

### Men's

brand  
\<%= text\_field 'detail', 'brand', :index=\>"gender=\>Mens" %\>

fabric blend  
\<%= text\_field 'detail', 'blend', :index=\>"gender=\>Mens" %\>

Mark Reginald J. wrote:

> Geoff D. wrote:
> 
> > new detail record with the right values (ie,  
> > product\_id=123456,gender=mens, size=XL, inventory=20)
> 
> All form helpers have an :index option that will separate  
> the posted parameters of each item by a separate hash id.  
> Note: Be careful to put the index option for select helper  
> calls in the html\_options hash rather than the options hash.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Geoff\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58f4c7/32.png) [@Geoff\_D](https://www.ruby-forum.com/u/Geoff_D)\
**Post date:** [July 17, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515/4 "2006-07-17T19:14:07Z")

</div>

Okay, this is getting me quite close to where I need to be. Thanks much

I’m going to have to parse this stuff in the model or controller, right?  
No way Ruby will automatically put subindexed items into the correct  
table?

---

<div class="post-metadata">

**Author:** ![Geoff\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58f4c7/32.png) [@Geoff\_D](https://www.ruby-forum.com/u/Geoff_D)\
**Post date:** [July 17, 2006, 7:07am UTC](https://www.ruby-forum.com/t/updating-multiple-records-with-form/98515/5 "2006-07-17T07:07:21Z")

</div>

Geoff D. wrote:

> ### Men's
> 
> brand  
> \<%= text\_field 'detail', 'brand', :index=\>"gender=\>Mens" %\>
> 
> fabric blend  
> \<%= text\_field 'detail', 'blend', :index=\>"gender=\>Mens" %\>

\<%= text\_field :details, :brand, :index =\> ‘mens’ %\>  
is the sort of thing you want, but since you really need two  
levels of indexing you may want to use text\_field\_tag instead:

\<% for gender in %w(mens, womens) %\>  
\<% for size in %w(S M L XL) %\>  
\<%= text\_field\_tag “inventory[#{gender}][#{size}]” %\>  
\<% end  
end %\>

Which will give you a inventory hash nested by gender and  
size.

If you also want the text boxes pre-filled with existing  
or previously-posted values, you’ll have to add a parameter  
to the text helpers to pick up the right value from either  
the details records or params hash.

–  
We develop, watch us RoR, in numbers too big to ignore.
