Forum: Ruby on Rails inserting multiple rows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
74bdb7628960d0bc1cc62647cd105eca?d=identicon&s=25 mizage (Guest)
on 2005-12-14 16: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]">
....
</select>

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

on and on...

how do i get the controller to take all the form elements and do a
multiple insert into the db?
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2005-12-14 18: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
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:

   <input type="hidden" name="entry[game_id]" id="entry_game_id"
value="3">
   <input type="hidden" name="entry[user_id]" id="entry_user_id"
value="19">

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
74bdb7628960d0bc1cc62647cd105eca?d=identicon&s=25 mizage (Guest)
on 2005-12-14 18: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]">
  ...
 </select

 <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

  ...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
74bdb7628960d0bc1cc62647cd105eca?d=identicon&s=25 mizage (Guest)
on 2005-12-14 20: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,
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
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2005-12-14 21: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.
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2005-12-14 23:20
(Received via mailing list)
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:

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