# Rails Entity-Relationship Diagrams

> In this short post, see how you can visualize your domain model quickly using the rails-erd gem.

- Canonical: https://dennisokeeffe.com/blog/2022-03-17-rails-entity-relationship-diagrams
- Published: 2022-03-17
- Updated: 2022-03-17
- Tags: ruby, ruby-on-rails

This post will look into the [`rails-erd`](https://github.com/voormedia/rails-erd) gem and how we can set it up to quickly visualize what our relationships in Rails look like.

Source code can be found [here](https://github.com/okeeffed/demo-rails-associations).

## Getting started

In this particular post, we will make use of another project worked on `demo-rails-associations` in ["Understanding Rails Associations"](https://blog.dennisokeeffe.com/blog/2022-03-16-understanding-rails-associations) and ["Through Rails Associations"](https://blog.dennisokeeffe.com/blog/2022-03-17-through-rails-associations):

```s
# Create a new rails project
$ git clone https://github.com/okeeffed/demo-rails-associations.git
$ cd demo-rails-associations

# Add the gem
$ bundle add rails-erd
```

After cloning the project, we just need to add the `rails-erd` gem and we are basically home and hosed.

## Generating the diagram

At this point, all we need to do to output a diagram is to run `bundle exec erd`.

That will output a PDF file `erd.pdf`. The output will outline the following from the relationships we created in the previous posts:

![Output ERD](https://cdn.dennisokeeffe.com/assets/2022-03-17-through-rails-associations/2-through-association.webp)

## Summary

Today's short post demonstrated how to generate the entity-relationship diagram for the Rails models using `rails-erd`.

## Resources and further reading

- [`rails-erd`](https://github.com/voormedia/rails-erd)
- [Source code](https://github.com/okeeffed/demo-rails-associations)
- ["Understanding Rails Associations"](https://blog.dennisokeeffe.com/blog/2022-03-16-understanding-rails-associations)
- ["Through Rails Associations"](https://blog.dennisokeeffe.com/blog/2022-03-17-through-rails-associations)

_Photo credit: [`pawel_czerwinski`](https://unsplash.com/@pawel_czerwinski)_
