Hacker News new | ask | show | jobs
by TallboyOne 4475 days ago
I prefer rails-ERD. annotate_models is also great.
1 comments

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.