I am presently designing a database schema for use in a Rails 3.1 application.
At the moment, I am using MySQL Workbench to design the schema visually, and then manually translating this to Rails migrations & models.
Can anyone indicate if there are any solutions that will allow a schema to be designed visually and translated automatically (i.e. via script) to Rails?
Thanks!
First off, the “database-first” approach definitely isn’t really the preferred way to work with Rails… but if you really want to…
If you generate the the tables from your schema you can configure the Rails app’s
config/database.ymlfile to connect to your database, then callrake db:schema:dumpwhich generates thedb/schema.rbfile from the database. Then you can create a migration and copy the code fromdb/schema.rbinto thechange(orself.up) method.Note that this does not automatically create model classes – you’ll have to create these yourself, remembering to
--skip migrationin therails generate model, and possibly needing to make liberal use of theset_table_name(to map the model class to the right table name),alias_attribute(to map model attributes to the right columns), and perhapsset_primary_key.There were some more complete approaches to this sort of thing for older versions of Rails (Magic Model Generator and reverse_scaffold are two that I’ve found), but I don’t know of any that have been upgraded to work with Rails 3.