Use rails-erd to generate model relationship diagrams in Ruby on Rails

Mav Tipi
2 min readOct 15, 2020

The Ruby gem rails-erd is very useful for both communication and testing purposes. You can use it to immediately see if you’ve set up your model fields and relationships correctly, and to show this structure to other members of your team.

on the left, a simple two-model domain; on the right, seven models with various relationships.

If you’re responsible for the back-end on a project other people are involved in (technically or otherwise), you’ll have to make such a diagram, either by hand or with a tool such as rails-erd. Furthermore, you can generate this to see if you’ve made a mistake somewhere as part of your testing suite.

Setup Instructions

rails-erd requires graphviz, an open-source graphing software. If you’re on OSX and have Homebrew, you can install graphviz in your terminal:

brew install graphviz

If you don’t have Homebrew, this is a fine time to change that. If you’re on another system, here’s graphviz’s download page.

After that go into your gemfile and add

gem 'rails-erd', group: :development

If you’re not working with groups in your gemfile, you can skip that part. Grouping gems allows you to install different sets of gems for different purposes; for example, you wouldn’t need this diagram-making gem in :production, just :development.

After that, run bundle install, and you’ve configured rails-erb. As long as you’ve set up your models already, you can now run your new rake task:

rake erd

to create your domain model diagram. It’ll spit it out as a PDF in your app folder:

You’ll immediately notice that you set fields as being the wrong data types and improperly specified your relationships! Woo!

--

--