# Pre-loading the database?

**URL:** <https://www.ruby-forum.com/t/pre-loading-the-database/114657>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657 "2007-09-17T13:23:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/1 "2007-09-17T13:23:43Z")

</div>

I am sure this has been asked before, but, I can’t seem to find an  
example.

Our app needs a bunch of data pre-loaded into the database. What is  
the ‘accepted’ way of doing this?

Do I put the data into a migration? The same migration that I create  
the table in?

thanks!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/2 "2007-09-17T13:24:20Z")

</div>

Technically, this is called “bootstrapping”.

I think usually it’s done with a rake task.

For example, my app comes with a rake task called db:bootstrap with  
populates my data base with all the default data it needs to start,  
e.g. default admin, etc…

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/3 "2007-09-17T13:25:10Z")

</div>

The best way I’ve seen is to create a Rake task of sorts (I was taught  
to use the task name ‘bootstrap’ which would let you call “rake  
bootstrap”).

–Jeremy

On 9/14/07, phil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks!
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/4 "2007-09-25T23:02:30Z")

</div>

Well, we’re only talking about 100 rows (10 in one table 90 in  
another) that won’t change.  
I was just going to put  
obj = Object.create(:attr =\> ‘blah’)  
in a migration file…  
It is only happening once, so speed is not a huge issue.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/5 "2007-09-17T13:25:25Z")

</div>

On Sep 14, 7:47 am, phil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am sure this has been asked before, but, I can’t seem to find an  
> example.
> 
> Our app needs a bunch of data pre-loaded into the database. What is  
> the ‘accepted’ way of doing this?
> 
> Do I put the data into a migration? The same migration that I create  
> the table in?
> 
> thanks!

it’s faster to use the bulk load, like MySQL’s LOAD DATA INFILE, or  
psql COPY. Every DBMS should have something with different names/  
switches

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 25, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/6 "2007-09-25T23:11:10Z")

</div>

I should have been a tad more clear on this:

Object.transaction do  
Object.create(:attr =\> ‘blah’)  
Object.create(:attr =\> ‘blah1’)  
Object.create(:attr =\> ‘blah2’)  
…  
end

William P. wrote:

> Object.transaction do
> 
> > > > Our app needs a bunch of data pre-loaded into the database. What is
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/pre-loading-the-database/114657/7 "2007-09-25T23:10:01Z")

</div>

That should work fine if speed is not an issue. You could also put it in  
a separate ruby scrip and use script/runner to load them. This would  
allow you a little more flexibility as you could delete and reload data  
without having to migrate the database back and forth each time.

One thing you can do to dramatically increase speed in the script runner  
approach is to wrap all of the inserts in on transaction rather than  
each having it’s own.

Object.transaction do  
Object.create(:attr =\> ‘blah’)  
end

phil wrote:

> > > the table in?

–  
Sincerely,

William P.
