# Inserting multiple rows

**URL:** <https://www.ruby-forum.com/t/inserting-multiple-rows/50256>\
**Category:** Rails\
**Created:** [December 14, 2005, 4:19pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256 "2005-12-14T16:19:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mizage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mizage](https://www.ruby-forum.com/u/mizage)\
**Post date:** [December 14, 2005, 4:19pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256/1 "2005-12-14T16:19:59Z")

</div>

I have table which stores multiple entries. Each row has user\_id,  
game\_id and a pick\_id. On a page the user has to make load of choices.  
All this gets submitted.  
the html looks like so:

.... ...

on and on…

how do i get the controller to take all the form elements and do a  
multiple insert into the db?

---

<div class="post-metadata">

**Author:** ![mizage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mizage](https://www.ruby-forum.com/u/mizage)\
**Post date:** [December 14, 2005, 6:15pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256/2 "2005-12-14T18:15:50Z")

</div>

mizage wrote:

> 

Let’s say your table which stores multiple entries is named ‘entries’  
and the model name is Entry.  
From what you posted I am thinking you have the following table  
structure and this whole post is  
based around this structure. If this is incorrect please post more data  
about your problem and your  
setup:

```
entries (tablename)
---------------
pick_id (column)
user_id (column)
game_id (column)

```

Since user\_id, game\_id and pick\_id all belong to table entries we take  
advantage of rails. Rename  
your html form elements:

- ‘game\_id’ to ‘entry\_game\_id’
- ‘user\_id’ to ‘entry\_user\_id’
- What field does pick[3] and pick[4] correspond to? Can they select  
multiple picks?

Also give your fields a name attribute:

- ‘entry[game\_id]’
- ‘entry[user\_id]’
- And possibly pick…i don’t know how you’re using that yet, so i  
can’t say for sure

So now your html looks like:

In your controller you can now do something like:

entry = Entry.new params[‘entry’]  
if entry.save  
flash[:notice] = ‘yay it saved!’  
else  
…  
end

Rails will parse the name/id tags of the inputted form elements and it  
will populate your Entry  
object for you if it follows the naming convention…  
“modelname\_fieldname” for the id tag and  
“modelname[fieldname]” for the name tag.

Hope this helps,

Zach

---

<div class="post-metadata">

**Author:** ![mizage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mizage](https://www.ruby-forum.com/u/mizage)\
**Post date:** [December 14, 2005, 6:36pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256/3 "2005-12-14T18:36:42Z")

</div>

the table is about that except for the id column (which auto increments)  
and name.

> ```
> picks (tablename)
> ---------------
> 
> ```

```
  id (column
  pick_id (column)
  user_id (column)
  game_id (column)

```

There is one pick per game\_id. So now the html is:

 ... \</select ... \</select

…plus another 40 fields which

the controller now does this:

```
pick = Pick.new(params['pick'])
pick.save

```

The first group of fields is saved to the db correctly. The other  
41 do not get saved. Do I need to loop in my controller?

cheers, etienne

---

<div class="post-metadata">

**Author:** ![mizage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mizage](https://www.ruby-forum.com/u/mizage)\
**Post date:** [December 14, 2005, 9:01pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256/4 "2005-12-14T21:01:42Z")

</div>

This is basically what I had to in a Java-equivalent program, and I  
don’t see any other solution. I am still new at Ruby though.

Cleanliness, you could create a helper for this, it takes all the  
params, you tell it which group to look for and it iterates through  
that group, saving it for that Model.

Thanks for posting your solution even though response was minimal, it  
helps others with the same problems.

- Nic.

---

<div class="post-metadata">

**Author:** ![mizage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mizage](https://www.ruby-forum.com/u/mizage)\
**Post date:** [December 14, 2005, 8:40pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256/5 "2005-12-14T20:40:31Z")

</div>

ok i figured it out but it doesn’t “feel” very clean!! so if anyone has  
a better way, please let me know. Newbie muppet code can always be  
improved upon.

The controller does this:

i = 0  
master = params[‘pick’] #returns arrays of pick\_ids, game\_ids,  
user\_id  
game\_ids = master[‘game\_id’] #returns array of game\_ids  
pick\_ids = master[‘pick\_id’] #returns array of pick ids  
user\_ids = master[‘user\_id’] #returns user\_id

game\_ids.each do  
pick = Pick.new  
pick.game\_id = game\_ids[i]  
pick.pick\_id = pick\_ids[i]  
pick.user\_id = user\_ids  
pick.save  
i += 1  
end

---

<div class="post-metadata">

**Author:** ![mizage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@mizage](https://www.ruby-forum.com/u/mizage)\
**Post date:** [December 14, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/inserting-multiple-rows/50256/6 "2005-12-14T23:20:36Z")

</div>

UPDATE:  
Checking the Agile WD book, they have a similiar example with  
checkboxes relating to order that have shipped (pg 121). They create  
numbered checkboxes based on the order\_id, and then parse through it  
in a loop in the Controller:

A sample HTML for the checkbox is:

Where the [1] portion is referring to the order\_id, meaning this is  
the dynamic part

So, in the controller, the code they have is:

to\_ship = params[:to\_be\_shipped]  
if to\_ship  
to\_ship.each do |order\_id, do\_it|  
if do\_it == “yes”  
# mark order as shipped (DB work)…  
end  
end  
end
