Preloading database tables

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

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?

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

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.

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

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

Best,

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