Forum: Ruby on Rails Preloading database tables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-05-24 14:19
(Received via mailing list)
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
68814ed988eee4f21b2cd07353e5e445?d=identicon&s=25 Mk 27 (mk27)
on 2009-05-24 14:58
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?
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-05-24 15:04
(Received via mailing list)
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
68814ed988eee4f21b2cd07353e5e445?d=identicon&s=25 Mk 27 (mk27)
on 2009-05-24 15:35
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.
4375b4aca04b8d72482fd409fb6d0b47?d=identicon&s=25 Roderick van Domburg (roderickvd)
on 2009-05-24 16:13
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
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-24 17:54
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
marnen@marnen.org
This topic is locked and can not be replied to.