Generate Entity-Relationship diagrams from Rails ActiveModels


Hey people! Documenting your code is really crucial for projects regardless of the size of the project. There are a lot of stuff that must be documented in a project. You have various tools to generate documentation for your code. I am going to show you a tool, a gem, that allows you to document your database models. When I was doing my Pacman (github.com/steverob/student-marks) I used an online uml tool called “dbdsgnr” to document my database. But I had to make many chances to my database schema and updating the Entity Relationship diagram I had on that website was getting tedious. At one point I dropped the whole idea of maintaining that piece of documentation. When I completed my project (pending testing), I wanted to document my database and I was searching for a tool that would document my rails project by reading the ActiveRecord models that I have. That was when I found this really cool gem called ‘rails-erd’.

This gem is maintained at github/voormedia/rails-rd and the project website is rails-erd.rubyforge.org. Installation of this gem is pretty easy. It depends upon a graphics tool called Graphviz.

You can install graphviz on Ubuntu using the command,

sudo apt-get install graphviz

Now add ‘rails-erd’ to your project’s Gemfile under the development group. So it would look like,
group :development do
gem "rails-erd"
end

Now run ‘bundle install’. This will enable your project to use the rails-erd gem.

We have installed the gem and its dependency. All we have to do now is to run a simple rake task to generate the ERD. The command is,

rake erd

This will generate “ERD.pdf” file in the application root. Neat huh? The best thing about this is that you can customize the way the output is generated by passing in many options. The various customizations are clearly explained in the project’s website. But mostly you wouldn’t need much customization. And I can tell you, the output is quite awesome. The entities are spaced well and the overall alignment of the diagram is quite brilliant. It looks like as though someone had manually aligned the entire thing. Okay, you’ll know what I am saying when you see the output pdf that I got for my project. Here it is. Click the image to get a better view.

See? Its just awesome. For more information regarding the gem visit rails-erd.rubyforge.org.

Steve

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s