104
91

More than 5 years have passed since last update.

Rails ERDの導入と実行

Last updated at Posted at 2014-08-24

パーフェクトRuby on Rails で紹介されていたので、使ってみました。
簡単に導入できるので、テーブル間の関係を素早く把握したい時には便利だと思いました。
公式サイト(http://rails-erd.rubyforge.org/)
がリンク切れになっており、実行オプションなどが参照できなくて困りましたが、ググってhelpを見たら何とかなりました。

準備

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', github: 'paulwittmann/rails-erd', branch: 'mavericks'
end

実行(デフォルト)

以下、実行すると、Railsのrootにerd.pdfが生成されます.

$ bundle exec erd

※ 以下だと失敗した

$ rake erd

出力されるER図の例(デフォルト)

erd.png

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.

実行(オプション付き)

プライマリーキーやcreated_atなどのカラムも出力するようにしました。
pdfではなく、名前を指定したpngファイルで出力するようにしました。

$ erd --attributes=foreign_keys,primary_keys,content,timestamp --filename=erd_sample --filetype=png

出力されるER図の例(オプション付き)

erd_sample.png

参考

104
91
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
104
91