Does Ruby on Rails have a way to import an existing database schema?
I know I can do a db:create 开发者_StackOverflowand create database tables or db:migrate to a new version, however what I want to do is import the schema from an existing database (all tables) and generate models for each one.
Does ActiveRecord have the ability to do this? Thanks!
Nope. ActiveRecord is an ORM, so it doesn't have anything to do with generating database schemas or generating models. Rails doesn't have any concept of doing this, either.
You'll have to write the database migrations and do this yourself.
There's kind of a way. Create a new Rails 2.2 app (it can't be 2.3 or 3.0, the gem hasn't been updated since 2007). Then install and use the Magic Model Generator gem.
This will look at your existing database and extra all the models from it, like magic. Since the structure of basic ActiveRecord model files is pretty similar in Rails 3, you can just copy them into your current project and tweak as necessary.
精彩评论