13 comments

[ 4.0 ms ] story [ 32.6 ms ] thread
Rubymine comes with such a feature built-in. However, I found that my model diagram was too complicated to really be useful.
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.
If you use MySQL, planning with MySQL Workbench provides similar results to what was shown (table to table relations).
RubyMine has this features built in and provides nice tools to manipulate the end diagram.
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.

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.