↩ More Magic

Magic Model Generator

Get Version 0.5.0

./script/generate magic_model


Imagine you get offered $5,000 to write a basic website/admin application on a legacy database that has 200 interconnected tables. You’ll need to add all the associations and validations based on the existing schema.

Imagine how long that would take! 1 day? 1 week? more?

Try 1 minute.

The Magic Model Generator does all this for you automatically.


$ sudo gem install magic_model_generator

Quick demo

Create a rails application, and point it to your database.

$ rails magic_show -d [mysql|sqlite|postgresql|oracle|etc|etc]
$ cd magic_show
$ cp database.yml.sample database.yml
and point it to your database.yml to your legacy database

In the video below I use the database created for the ActiveRecord test cases – activerecord_unittest. If you’ve never downloaded the activerecord gem, run:

$ rake build_[mysql|sqlite|postgresql|oracle|etc|etc]_databases
$ rake test_[mysql|sqlite|postgresql|oracle|etc|etc]

Now install the magic_model_generator gem:

$ sudo gem install magic_model_generator

By the way, that’s all the preparation I did for the video.

Now recreate the schema.rb file and the schema_info database table via

$ rake db:migrate

Finally, run the generator:

$ ./script/generate magic_model


Currently rails_generators cannot merge results into existing files, only skip or overwrite. I am investigating a fix for this so that you can use the MMG for iterative development.

Related articles

Similar ideas

The Magic Show at RejectConf

Dr Nic’s Blog

http://www.drnicwilliams.com – for future announcements and other stories and things.


Discussion about the Magic Multi-Connections is on the Magic Models forum:


How to submit patches

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.

The trunk repository is svn://rubyforge.org/var/svn/magicmodels/magic_model_generator/trunk for anonymous access.


This code is free to use under the terms of the MIT licence.


Comments are welcome. Send an email to Dr Nic Williams.

Dr Nic, 1st June 2007
Theme extended from Paul Battley