Rails erd, What is it?
Rails erd is a Ruby Gem that allows you to generate a diagram based on your application’s Active Record models. Erd provides an easy way to view all of the relationships in your app. The diagram created by the gem, maps model relationships, allowing you to easily check and ensure that your application is set up correctly.
How To Set It Up
While most Ruby gems are easy to install, Ruby erd has a few quirks to it. These few extra steps are easy enough to work through.. The first thing the gem requires is Graphviz 2.22+. to install this you can simply run
brew install graphviz . This is the only dependency you need to install on your own, everything else will be downloaded in the gemfile.
The next step is adding the gem to your gemfile. The easiest way to do this is to go to the erd gems readme at https://voormedia.github.io/rails-erd/install.html and copy and paste required code into your gemfile.
Once you have it in your gemfile, you can run the bundle install and it will download and install all the proper files needed to run.
If the file you’re working on does not require Ruby on Rails, there is a fairly easy work around that allows you to use the gem even though your not using rails. This work around can also be found in the readme and requires that we add a little bit of code to the Rakefile.
If you have done everything correctly, it should show up in console when you enter rake -T.
Once it is all set up, make sure you have all of your models set up as well as
rake db:migrate and created all of your tables. Then when you’re ready, call the gem with the
rake erd command. This will create a pdf called erd.pdf, and it will be saved among the other files in your domain model.
Now that we have the gem set up, let’s see what it can do. Here is a working example based on a project I recently worked on.
For example, take Captain FatCat and his boat HMS Feline, it is now easy to track how this information will flow though the model.
This project had four models. The captain has many boats, boats has many BoatClassifications and Classifications also has many BoatClassifications. With erd it helps you visualize all of this so it is easy to understand.
The gem also includes many customization options that can be found at https://voormedia.github.io/rails-erd/customise.html. These options allow for you to include indirect relationships which are defined in Active Record by has_many :through associations.
rake erd indirect=true
The gem will also remind you what your keys are where each key is Foreign or Primary. This is done by calling rake erd and adding the attributes=foreign_keys,primary_key to the end of it.
rake erd attributes=foreign_keys,primary_key
When your models become larger and larger it will be harder to keep track of what you have entered into active record and how it all connects. The Rails erd gem will ensure that all your models are connected in the way you planed for them to be and will keep your models from falling apart and breaking.