Inserting lots of data at once

Is there a way in Rails to insert many records at once, with a single
SQL command? I’ve noticed that loading a dump file of data from the
command line is much faster to doing something like
SomeModelClass.create(…) for each individual record.
–Paul

On 2/6/08, Paul E. G. Lynch [email protected] wrote:

Is there a way in Rails to insert many records at once, with a single
SQL command? I’ve noticed that loading a dump file of data from the
command line is much faster to doing something like
SomeModelClass.create(…) for each individual record.

create accepts an array of hashes as well as a single hash.

http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001379


Greg D.
http://destiney.com/

On 6 Feb 2008, at 23:15, Greg D. wrote:

http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001379

Which won’t make any speed difference since if you pass it an array of
hashes it just iterates over that array calling create on each element.
In times like these I’ve occasionally fallen back to just creating an
sql statement to insert all the values and executing it. You lose AR
type conveniences(validations etc…) but it is a lot faster on large
numbers of records.

Fred

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