Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Visualizing Rails Schemas with the RailRoady Gem (coachup.com)
36 points by ar4420 on March 12, 2014 | hide | past | favorite | 13 comments


This is a side tangent but seeing graphviz being used here reminded me of this total boss ruby patch: http://cirw.in/blog/find-references

I had spent a week slowly isolating this brutal memory leak until being able to see the whole ObjectSpace illuminated my problem.


DataMapper has this as dm-visualizer as well in case you use that over AR in rails. https://github.com/postmodern/dm-visualizer


I just yesterday found the rails-erd gem to accomplish the same task. https://github.com/voormedia/rails-erd


The problem I have with these tools is that the diagram it produces is really not that great. I guess it tells you what models you have, the columns they have, and whether a model is tied to another one... but not much else.

Here's the thing: I already know how many models I have (I just look in app/models) and which columns back them (I just look in db/schema).

The thing I often want that 1) ActiveRecord doesn't give me and 2) I haven't seen in any tool yet is a diagram of the relationships. So I have two models, A and B, and A has many B. How do you diagram that? Typically I've seen arrows: B points to A, with the words "has many" written next to it. The thing is, if I have 30 models in my app, I'm not going to be able to read this at a glance. Words are too small. Diagrams are meant to be rough pictures.

The way I visualize this in my head and consequently diagram this on a whiteboard is, multiple instances of B point back to A. It's easy. If you have a has_one instead, then only one B points back to A. Makes sense right? No one does this. I often wonder why.


I prefer rails-ERD. annotate_models is also great.


Instead of annotate models, I have a function in my .irbrc/.pryrc that will print out the columns for me when I'm in a rails console (I usually have one open almost all of the time). I prefer this to cluttering my model files.

  [6] pry(main)> c Account
  Columns for 'accounts'
    id                              :integer
    created_at                      :datetime
    updated_at                      :datetime
    address                         :string
    city                            :string
    state                           :string
    zip                             :string
    phone_number                    :string
    last_ip                         :string

  # Print formatted column names and type info for ActiveRecord models
  def fields(model)
    puts "Columns for '#{model.table_name}'"
    width = model.columns.inject(0) { |max_width, col| col.name.size > max_width ? col.name.size : max_width }
    _columns = model.columns.collect do |c|
      "  %-#{width + 2}s :%s" % [c.name, c.type]
    end.join("\n")
    puts _columns
  end
alias :c :fields


Do you keep the rails console running constantly? It seems more inconvenient to start and stop the rails console than navigate files for your schema, which is also searchable by virtue of it being written down.


Not really, it's just usually when I'm trying out different queries in the console that I have the, "oh crap, what did I name that column" moments. If I need to search for something in particular, I just search schema.rb.


RubyMine has this features built in and provides nice tools to manipulate the end diagram.


Rubymine comes with such a feature built-in. However, I found that my model diagram was too complicated to really be useful.


If you use MySQL, planning with MySQL Workbench provides similar results to what was shown (table to table relations).


DbWrench for Mac along with PgAdmin is another good alternative for doing this with a Postgres database.





Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: