Save item to DB x times (howto)?


#1

Hi All,

I have a pretty simple question. I have a view that offers the user the
following:

  1. A text field to enter in a passphrase
  2. Another text field to define how many times that phrase is written
    to the DB

It probably doesn’t make sense for it to be written to the DB x times,
but
this is the first step in a process and the passphrase will become
unique.

I currently don’t have a problem saving one phrase to the database at a
time, but I am a little unclear on how, at the controller level, to have
it
post to the database x times. I am stumped on how I’d have the text
field
(#2 above) look in the view and how I’d access it and then complete a
for
loop over it and save a new row to the table each time until the count
is
reached.

Any ideas on how I’d be able to do this? Thanks in advance!


#2

On 4/17/06, Ryan P. removed_email_address@domain.invalid wrote:

I currently don’t have a problem saving one phrase to the database at a
time, but I am a little unclear on how, at the controller level, to have it
post to the database x times. I am stumped on how I’d have the text field
(#2 above) look in the view and how I’d access it and then complete a for
loop over it and save a new row to the table each time until the count is
reached.

Any ideas on how I’d be able to do this? Thanks in advance!

I have no idea why you’d want to do this, but I’ll assume you can
handle yourself. Heh.

Assuming your view has something like:
<%= text_field ‘passphrase’, ‘save_count’ %>

In the controller action that posts to, you can say:
count = params[:passphrase][:save_count].to_i rescue 0
count.times do
Passphrase.create params[:passphrase]
end