Can I create models from a preexisiting database?

Hello All this is my first post here, I have been trying to learn rails
and kept hitting dead ends because of outdated tutorials then I went
through the official one and what do you know it works! :wink: .

I’m sure this is a noob question but I can’t find the answer searching
for it for some reason. Can I do a reverse migration where instead of
creating the database scema based on my models, I generate models
(migrations and controllers too) based on the database I already have
laid out (MySql). I remember trying one tutorial a few months ago that
did it this way but then none of the C.R.U.D. features worked right. I’m
using the latest official versions ruby 1.8.7 and RoR 2.3 .

Thanks.

[email protected] Huh wrote:

using the latest official versions ruby 1.8.7 and RoR 2.3 .

Thanks.

I don’t know of any automatic way to generate models from an existing
schema, but as far as I know, there’s nothing to stop you generating
models normally using the existing database structure.

I guess you can then just “forget” to run the migrations, and your
models will work with your existing data.

It might not be totally the best way to do it, but it should work.

HTH

~Matt

Matt H. wrote:

I don’t know of any automatic way to generate models from an existing
schema, but as far as I know, there’s nothing to stop you generating
models normally using the existing database structure.

Right. However, you’ll want an accurate schema.rb file anyway. Use
rake db:schema:dump to generate one.

I guess you can then just “forget” to run the migrations, and your
models will work with your existing data.

No need to “forget”. Use --skip-migration, or delete the migrations
without running them, or don’t use the model generator at all!

Best,

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