Keeping technical documentation handy is extremely useful, especially when a project gets huge in terms of the code and database. One of the documents that should be included in the documentation is an Entity Relationship Diagram, or ERD. This article will discuss how to generate ERD at any stage of a project.
Create a new project.
1
rails new erd
Now install dependencies.
If you are a mac user:
1
brew install graphviz
On Linux, run:
1
sudo aptitude install graphviz
Now add the following gem in the development group:
1
gem "rails-erd"
Run bundle install
, then generate a few models.
1
2
3
4
rails g model User name email
rails g model Post title body:text user:belongs_to
rails g model Comment body post:belongs_to
rails db:migrate
Now define relationships. In app/models/user.rb
, put the following line:
1
2
3
# app/models/user.rb
has_many :posts
In app/models/post.rb
, put the following line:
1
2
3
# app/models/post.rb
has_many :comments
Now run rake erd
and it will generate an Entity Relationship Diagram. Happy ERDing!