Command line utility to interrogate models
Posted by Tammer Saleh
Oct 19
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.
Comments on this post
Nov 26
Josh Adams said,
Have you seen Rails Application Visualizer?Dec 23
Blake Watters said,
I have fixed a couple of issues in the script. First, I was getting nil errors coming out of the direct_ancestors method. I also have some heavy setup in my environment.rb file that forces some models to load, so I added an explicit whitelisting of models as they are loaded out of the app/models subdirectory to prevent models from being 'hidden'. Thanks for the excellent tool! My patch and updated version are available at http://thatswhatimtalkingabout.org/news/2006/12/23/improved-show_model-scriptSorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Come “ride the toad” on Hoptoad, the app error app.
Thunder Thimble: Brand monitoring for social media.
Widgetfinger: Simple content management for simple websites.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our beginning or advanced training.
Archives