See more posts

generate isolated entity diagrams in rails

Rails ERD Is a great tool for inspecting the database relationships in rails applications, but in large apps this gets unwieldy. This handy trick allows you to look at one central model and inspect the relationships that it defines automatically.

Gemfile

ruby
gem "rails-erd"

bin/generate_erd

ruby
#!/usr/bin/env ruby

# Ensure rails is loaded
exec("rails", "runner", __FILE__, *ARGV) unless defined?(Rails)

# Usage: generate_erd CamelCaseModelClass

model_class = ARGV[0].constantize
associations = model_class.reflect_on_all_associations.map(&:class_name).join(',')
exec("rails", "erd", "only='#{associations}'")

Thanks for reading! See more posts Leave feedback