Forum: Ruby on Rails Can I create models from a preexisiting database?

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.
87ad7f8e13450aad2b4b2999c5880d27?d=identicon&s=25 Rubytuesday@baje.us Huh (user_zero)
on 2009-06-02 22:10
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! ;) .

 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.
8fa0f5c2bf8ad238baa4e20cb0360ebd?d=identicon&s=25 Matt Harrison (iwasinnamuknow)
on 2009-06-03 00:11
(Received via mailing list)
Rubytuesday@baje.us 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
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-06-03 00:26
Matt Harrison 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
marnen@marnen.org
This topic is locked and can not be replied to.