While looking for a tool that’d help me to generate the database schema diagram, I’ve found RailRoad Ruby gem. However, it was not working with my Rails 2.3.5 + Ruby 1.8.7 on Windows. While running railroad in command line, I was getting an error:
c:/Ruby/lib/ruby/gems/1.8/gems/railroad-0.5.5/lib/railroad/app_diagram.rb:54:in 'reopen': No such file or directory - /dev/null (Errno::ENOENT) (...)
This seemed to me as an error similar to RoR: plugin install from GIT repository in Windows error. And luckily it was!
Solution
The soultion is as follows:
- Go to %ruby_installation_directory\lib\ruby\gems\1.8\gems\railroad-0.5.5\lib\railroad\ and open app_diagram.rb in a text editor.
(%ruby_installation_directory is c:\Rails by default) - Around line 54 find:
STDOUT.reopen(PLATFORM =~ /mswin/ ? "NUL" : "/dev/null")
and change it to:
#STDOUT.reopen(PLATFORM =~ /mswin/ ? "NUL" : "/dev/null") STDOUT.reopen('NUL:')
- Open controllers_diagram.rb in the same directory with a text editor.
- Around line 39 find:
require "app/controllers/application.rb"
and change it to:
require "app/controllers/application_controller.rb"
- That’s all! Now you can create for example your Rails project models schema (having Graphvis installed) just typing in command line:
railroad -a -i -M | dot -Tpng > models.png
Create tasks
To automate the process of creating schemes, you can create Rails task. In your rails_project_dir/Libs/tasks create diagrams.rake and copy the text:
namespace :doc do namespace :diagram do task :models do sh "railroad -a -i -M | dot -Tpng > doc/models.png" sh "railroad -i -l -a -m -M | dot -Tsvg doc/models.svg" end task :controllers do sh "railroad -i -l -C | neato -Tsvg > doc/controllers.svg" end end task :diagrams => %w(diagram:models diagram:controllers) end
Now you can run the task by typing in console: rake doc:diagrams to generate all diagrams (for models an controllers) or rake doc:diagram:models to generate diagram for models only.
Due to the information on RailsRoad Docs, there is a bug in Graphvis while crating svg graphic. There is a simple way to correct it (citation from RailsRoad Docs):
Important: There is a bug in Graphviz tools when generatingSVG files that cause a text overflow. You can solve this problem editing (with a text editor, not a graphical SVG editor) the file and replacing around line 12 „
font-size:14.00;
” by „font-size:11.00;
„, or by issuing the following command (see „man sed
„):sed -i 's/font-size:14.00/font-size:11.00/g' file.svgNote: For viewing and editing SVG there is an excellent opensource tool called Inkscape (similar to Adobe Illustrator.) For DOTprocessing you can also use Omnigraffle (on Mac OS X).