# Inserting lots of data at once

**URL:** <https://www.ruby-forum.com/t/inserting-lots-of-data-at-once/128346>\
**Category:** Rails\
**Created:** [February 6, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/inserting-lots-of-data-at-once/128346 "2008-02-06T23:40:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 6, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/inserting-lots-of-data-at-once/128346/1 "2008-02-06T23:40:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 7, 2008, 12:16am UTC](https://www.ruby-forum.com/t/inserting-lots-of-data-at-once/128346/2 "2008-02-07T00:16:13Z")

</div>

On 2/6/08, Paul E. G. Lynch [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001379)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 7, 2008, 9:46am UTC](https://www.ruby-forum.com/t/inserting-lots-of-data-at-once/128346/3 "2008-02-07T09:46:44Z")

</div>

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

> [Peak Obsession](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
