# Preloading database tables

**URL:** <https://www.ruby-forum.com/t/preloading-database-tables/168691>\
**Category:** Rails\
**Created:** [May 24, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691 "2009-05-24T14:19:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [May 24, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691/1 "2009-05-24T14:19:09Z")

</div>

Hello,

as I write a little cms system, I need to preload some table with  
data. I don’t know, if ‘preloading’ is the right name for this. I  
know, that I can write into some tables right in the migration file,  
but I want to have the predefined records in a seperate file, like you  
have with fixtures. How do you handle this problem?

Thank you for any suggestions!  
ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [May 24, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691/2 "2009-05-24T14:58:11Z")

</div>

Not sure what a fixture is; do you mean you want two seperate db files,  
or do you mean you want to populate the database outside of rails, and  
then use it?

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [May 24, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691/3 "2009-05-24T15:04:52Z")

</div>

Thanks for your answer, I’ll explain: I’ve got my migrations where I  
can set up the table layout. Afterwards I want to fill this database  
with some predefined records. These records should lay somewhere else  
in simple text files, CSV, XML or whatever. It’s just about to divide  
the table definitions and the data I want to add directly after the  
creation.

Thanks for your help,  
ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [May 24, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691/4 "2009-05-24T15:35:37Z")

</div>

ms wrote:

> Thanks for your answer, I’ll explain: I’ve got my migrations where I  
> can set up the table layout. Afterwards I want to fill this database  
> with some predefined records. These records should lay somewhere else  
> in simple text files, CSV, XML or whatever. It’s just about to divide  
> the table definitions and the data I want to add directly after the  
> creation.
> 
> Thanks for your help,  
> ms

You can create a database anyway you like and use it to replace the  
(empty) one initially created – as long as all the table and column  
names match, of course. You can even do that while the server is  
running. Also, every table must have a column “id” which is the INTEGER  
PRIMARY KEY, and this should be a unique number for each row. Table  
names should correspond to controller class names. They should be  
plural and capitalized\*, eg, if you have an app/views/document (and an  
app/controllers/document\_controller.rb), you will need a table in your  
database called Documents which corresponds to the entry in  
db/schema.rb.

Is that what you mean?

\*they may not have to be since they are not in the schema – anyway,  
when I’ve done this they were capitalized and it works fine.

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [May 24, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691/5 "2009-05-24T16:13:07Z")

</div>

ms wrote:

> Thanks for your answer, I’ll explain: I’ve got my migrations where I  
> can set up the table layout. Afterwards I want to fill this database  
> with some predefined records. These records should lay somewhere else  
> in simple text files, CSV, XML or whatever. It’s just about to divide  
> the table definitions and the data I want to add directly after the  
> creation.

You mean seeding? There’s a couple of plugins for this, like seed\_fu.  
You could also use fixtures and load those using rake db:fixtures:load.  
Finally, seeding will be supported out of the box in upcoming Rails  
releases.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [May 24, 2009, 5:54pm UTC](https://www.ruby-forum.com/t/preloading-database-tables/168691/6 "2009-05-24T17:54:52Z")

</div>

Roderick van Domburg wrote:  
[…]

> You mean seeding? There’s a couple of plugins for this, like seed\_fu.

Yup. There’s also a Railscast on the subject.

> You could also use fixtures and load those using rake db:fixtures:load.

Yeah, but that’s kind of ugly for large amounts of seed data…

> Finally, seeding will be supported out of the box in upcoming Rails  
> releases.

Really? Cool! That’s good news.

> –  
> Roderick van Domburg  
> [http://www.nedforce.com](http://www.nedforce.com)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
