rails-ERDを使用して簡単にER図を作成する


はじめに

職場でRailsで作られたプロダクトの最新のER図を作成しようということがあり、面倒なのでいいライブラリがないかなと調べたところrails-erdがあったので共有する。

準備

まず、OSにgraphvizというツールパッケージをインストールする。
Graphviz (Graph Visualization Software)とは
DOT言語のスクリプトで示されたグラフを描画する。
グラフ記述には十分な自由度が用意されていて、矢印ひとつにしても多くのバリエーションの中から適したものを選択できる。
https://ja.wikipedia.org/wiki/Graphviz

今回は、このグラフ描画を簡単にするツールを使用するgemみたいです。

早速入れて見ましょう。

Ubuntu

$ sudo apt-get install graphviz
Gemfile
group :development, :test do
  gem 'rails-erd'
end

Mac

$ brew install graphviz
Gemfile
group :development, :test do
  gem 'rails-erd'
end

実行

以下をプロジェクトのディレクトリで実行します。

$ bundle exec erd

ER図

オプション

help

$ erd -h

Usage: erd [-v]


        --title=TITLE                Replace default diagram title with a custom one.
        --notation=STYLE             Diagram notation style, one of simple, bachman, uml or crowsfoot.
        --attributes=TYPE,...        Attribute groups to display: content, primary_keys, foreign_keys, timestamps and/or inheritance.
        --orientation=ORIENTATION    Orientation of diagram, either horizontal (default) or vertical.
        --inheritance                Display (single table) inheritance relationships.
        --polymorphism               Display polymorphic and abstract entities.
        --direct                     Omit indirect relationships (through other entities).
        --connected                  Omit entities without relationships.
        --only                       Filter to only include listed models in diagram.
        --exclude                    Filter to exclude listed models in diagram.


        --filename=FILENAME          Basename of the output diagram.
        --filetype=TYPE              Output file type. Available types depend on the diagram renderer.
        --no-markup                  Disable markup for enhanced compatibility of .dot output with other applications.
        --open                       Open the output file after it has been saved.


        --help                       Display this help message.
        --debug                      Show stack traces when an error occurs.
    -v, --version                    Show version and quit.