Forum: Ruby on Rails inserting multiple rows

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
mizage (Guest)
on 2005-12-14 17:19
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:

<input type="hidden" name="game_id" value="3">
<input type="hidden" name="user_id" value="19">
<select name="pick[3]">

<input type="hidden" name="game_id" value="4">
<input type="hidden" name="user_id" value="19">
<select name="pick[4]">

on and on...

how do i get the controller to take all the form elements and do a
multiple insert into the db?
zdennis (Guest)
on 2005-12-14 19:15
(Received via mailing list)
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

    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:

   <input type="hidden" name="entry[game_id]" id="entry_game_id"
   <input type="hidden" name="entry[user_id]" id="entry_user_id"

In your controller you can now do something like:

   entry = params['entry']
     flash[:notice] = 'yay it saved!'

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,

mizage (Guest)
on 2005-12-14 19:36
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:

 <input type="hidden" id="pick.game_id" name="pick[game_id]" value="1">
 <input type="hidden" id="pick.user_id" name="pick[user_id]" value="5">
 <select id="pick_pick" name="pick[pick]">

 <input type="hidden" id="pick.game_id" name="pick[game_id]" value="2">
 <input type="hidden" id="pick.user_id" name="pick[user_id]" value="5">
 <select id="pick_pick" name="pick[pick]">
 </select another 40 fields which

the controller now does this:

    pick =['pick'])

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
mizage (Guest)
on 2005-12-14 21:40
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,
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.game_id = game_ids[i]
	pick.pick_id = pick_ids[i]
	pick.user_id = user_ids
	i += 1
Nic W. (Guest)
on 2005-12-14 22:01
(Received via mailing list)
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.
Nic W. (Guest)
on 2005-12-15 00:20
(Received via mailing list)
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:

<input name="to_be_shipped[1]" type ="checkbox" value="yes"/>

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)....
This topic is locked and can not be replied to.