Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

Command line utility to interrogate models

One of the projects I’m working on here at thoughtbot has some pretty hairy interrelated models on the back end. So, as both an exercise in meta-programming, and to help me get a handle on the code I’m working with, I’ve written a little command line script that dumps all kinds of information about a model (or about the general hierarchy of all the models).

If you run the script with no arguments, it prints a little tree showing the object hierarchy:


# show_model
ActionMailer::Base
    Notifier
ActiveRecord::Base
    Tag
    Posting
        Comment
        Entry
    Preferences
    User
    Group

Note that, in this example, comments and entries are subclasses of Postings (using single table inheritance).

More useful, though, is getting a quick reference on what class methods, instance methods, associations, and table columns a model has:


User < ActiveRecord::Base
  Class Methods:
    login
  Instance Methods:
    has_access_to?
    password_confirmation
    password_confirmation=
    posts_about
    tags
  Associations:
    has_many   Comments
    has_many   Entries
    has_many   Groups
    has_many   Postings
    has_many   Postings as non_comment_postings
    has_one    Preferences as preferences
  DB Columns:
    active     (boolean)
    admin      (boolean)
    birth_date (date)
    created_on (datetime)
    email      (string)
    first_name (string)
    id         (integer)
    last_login (datetime)
    last_name  (string)
    password   (string)
    prev_login (datetime)
    updated_on (datetime)

Anyways, I thought I’d post this in case it helps anyone else. You can download it here. Should also say that it currently assumes that you’re in the base RAILS_DIR directory.

Next up is a rails2dot that doesn’t suck.


About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.