Create 1000 items in DB

Hi,

I want to create 1000 items at once(vouchers) in my db. My class name is
Voucher. How can ik realize this…?

remco

and there is always the obvious solution:

1000.times do
Voucher.create(:attribute1 => ‘value1’)
end

MaD wrote:

and there is always the obvious solution:

1000.times do
Voucher.create(:attribute1 => ‘value1’)
end

In this case, however, the obvious solution is not the best: each call
to Voucher.create is a separate DB query. It’s far better to create one
query to insert all the objects; unfortunately, Rails makes this
somewhat difficult. You can either use a plugin such as ar-extensions
or build the SQL yourself.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hey Remco,

What format is your input data in?
Will this happen regularly or is this a one-time insert?

It sounds like you might want to look at ActiveRecord Extensions:
http://github.com/zdennis/ar-extensions/tree/master

Regards,
Gustav P.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs