LoginSignup
1
0

More than 1 year has passed since last update.

【Ruby on Rails】RailsアプリケーションのER図の自動生成

Posted at

はじめに

Railsなどを中心に勉強中のエンジニア初心者が他の記事を参考にしたり、実際に実装してみたりして、アウトプットの一環としてまとめたものです。
間違っていることもあると思われるので、その際は指摘いただけると幸いです。

ER図

ER図とは「DB上のテーブル同士の関連性をまとめた図」のこと。

DBの関連性を定義するにはbelongs_tohas_manyなどをモデルに記載するが、それらの関連性を図としてまとめたものがER図だある。

ER図を確認することでそのプロジェクトのDB構造やModelおよびテーブル間の関連性が一目で把握できるため、プロジェクトに参画する場合は、まずはこのドキュメントから目を通すことが多い。

rails-erdの導入

rails-erdというGemを使うことによって、簡単にアプリケーションのER図を生成することが可能となる。

Railsアプリケーションの設定変更

config/environments/development.rbconfig.eager_loadの値をtrueに設定する

config.eager_load = true

※ER図の出力が終わったら、このオプションをfalseに戻しておく。
eager_loadオプションをtrueにしたことが原因で「開発環境でのサーバー起動中にクラスを編集するとエラーが出る」ことがあるらしい。

Graphvizのインストール

rails-erdを実行するために必要となるツール。

docker環境の場合は、dockerコンテナ内で実行する必要があるため注意

実行コマンド

apt-get install doxygen doxygen-gui graphviz

ER図の出力

全てのER図の出力

下記のコマンドでER図を出力することができる。出力先のフォルダ名とファイル名は自由に設定できる。

実行コマンド

bundle exec erd --filename=フォルダ名/ファイル名

実行結果

tmpフォルダにerd.pdfというファイルが出力される。

% bundle exec erd --filename=tmp/erd
Loading application in 'test'...
DEPRECATION WARNING: Spree::Order state machine defined in Spree::Order::Checkout is deprecated. Future versions of Solidus will use Spree::Core::StateMachines::Order} (called from <top (required)> at /test/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.raise_with_invalid_currency set to true is deprecated. Please note that by switching this value, Spree::LineItem::CurrencyMismatch will not be raised anymore. (called from <top (required)> at /test/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.consider_actionless_promotion_active set to true is deprecated. Please note that by switching this value, promotions with no actions will be considered active. (called from <top (required)> at /test/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.run_order_validations_on_order_updater set to false is deprecated and will not be possibile in Solidus 3.0. Please switch this value to true and check that everything works as expected. (called from <top (required)> at /test/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.use_legacy_address_state_validator set to true has been deprecated and will be removed in Solidus 3.0. The Spree::Address state validation has been extracted into a configurable external class. Switch Spree::Config.use_legacy_address_state_validator to false to start using the external validation class. (called from <top (required)> at /test/config/environment.rb:5)
Generating entity-relationship diagram for 151 models...
Warning: Ignoring invalid model Spree::PromotionRuleRole (table spree_promotion_rule_roles does not exist)
Warning: Ignoring invalid association :creditable on Spree::Reimbursement::Credit (polymorphic interface Creditable does not exist)
Warning: Ignoring invalid association :originator on Spree::StockMovement (polymorphic interface Originator does not exist)
Warning: Ignoring invalid association :originator on Spree::StoreCreditEvent (polymorphic interface Originator does not exist)
Warning: Ignoring invalid association :zoneable on Spree::ZoneMember (polymorphic interface Zoneable does not exist)
Diagram saved to 'tmp/erd.pdf'.

一部のER図の出力

全てのER図を出力すると、テーブル数が少し多すぎて、いきなり全体を頭にいれるのは難しい場合もある。そのような場合は、確認したいModelおよびテーブルだけを抜き出してER図を生成することも可能。

実行コマンド

 bundle exec erd \
   --only='Spree::Taxonomy,Spree::Product,Spree::Taxon,Spree::Classification' \
   --filename=tmp/erd

公式リポジトリ

1
0
2

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
1
0