Dead Simple Model Diagrams for Your Rails Project

While working on Rails project I often find myself wanting a visual representation of my model classes.  I usually grab a notebook and manually write them out. Depending on the project, it can take time.

I started searching for a diagramming tool that might be easier and faster than writing out by hand, there are a bunch of them out there.  Most have a steep learning curve and are expensive.

A bit of searching around the web for a Ruby-specific tool lead me to a gem named rails-erd.  Maybe you have heard of it, maybe I’m the last to know, but regardless it is a nice find.

Installing

The gem relies on GraphViz to do it’s drawing magic.  There are a multitude of ways to install it, I used Homebrew:

brew install graphviz

Add the gem to your development group in your Gemfile:

group :development do
 gem 'rails-erd'
end

Don’t forget to run the bundle command.

When everything is install, from the root of your Rails project simple run:

rake erd

When the rake task runs, watch the output from the tool. It tells you items you won’t find on the diagram either because it’s not used or a relationship isn’t right.

The Output

The result will be a PDF file in the root of your project that looks something like this: 

Erd

As you can see, it gives a very nice model diagram with all the relations and properties. Just what I was looking for.

The tool is very customizable and the web site outlines everything that can be changed.  I haven’t looked very much at this aspect since it produced everything I needed the first time.

  • http://twitter.com/ohforfs Kevin McCaughey

    I get really annoyed at the amount of Rails developers that assume we work on Macs. Macs make up less than 10% of the user base and I wouldn’t touch one with a barge pole! ;)

  • http://www.accidentaltechnologist.com Rob Bazinet

    I think it’s safe to say most Rails developers are on Macs and some on Linux. Are you not a Rails developer? If you are one, are you on Windows?

  • http://www.facebook.com/michel.pigassou Michel Pigassou

    The RubyMine IDE also has a built-in diagrams generator.

  • http://www.accidentaltechnologist.com Rob Bazinet

    Yes, it does. I tried them out but didn’t really like the output. I wanted something easy to configure, easy to recreate the diagram and easily fit on a page in landscape to print. RubyMine didn’t seem very flexible and once I started trying to get it to do what I want and I was spending too much time for a simple task, I decided it wasn’t for me. If you use RubyMine on a regular basis then it might make sense to use the tool you know.

    I like rails-erd because it’s fast to run and since most of my development is using Sublime Text 2, it made the choice easy.

  • http://www.saturnflyer.com/ saturnflyer

    Great post, Rob!

    There’s also https://github.com/preston/railroady but I haven’t tried that

  • http://blog.8thcolor.com/ Martin Van Aken

    Nice, works as advertised, install in one line, add a gem to Gemfile & call. Another usage of one of my preferred tools, GraphViz

  • http://www.accidentaltechnologist.com Rob Bazinet

    Thanks Jim!

    I did briefly check out Railsroady but the requirements were a bit more. I may take a look at it to see if there are advantages to using it.

  • Robby Rob

    Hmm, seeing as rails is most frequently used on unix based systems, OSX and Linux fits the bill. Until recently, it was pretty tedious to even get a working version of Ruby, Rails, and gems on a Windows machine. So, your figure about user base, means nothing when talking about a specific Framework and specific development environments.

  • picksio

    A quick look at documentation and sample project use both gems, I do prefer gem ‘railroady’:

    - ERD + UML

    - more output format options.

  • MarkJones

    Agreed – my development and deploy platforms are identical (Ubuntu). Having to learn and maintain a separate development environment is a big overhead.

  • andrekibbe

    Most Rails developers, as you’ll see by going to any Rails conference, use Macs, but there’s an MSI installer for GraphViz if you want to use Windows.

  • http://www.lefrois.com/lefrois-general-contracting.aspx design/build ny

    Keep updating the blog, looking forward for more contents…