The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

A gem to help you document your Rails ActiveRecord model

with 2 comments

I’ve been using the “annotate” gem for a while and it’s simple whilst incredibly useful. Essentially once run it documents each resource in your database within the appropriate file. Here’s how to get it working.

Step 1

Install the gem. There are a few ways to do this but I usually include the following line in my Gemfile:

gem 'annotate'

Then run “bundle install” from the command line.

Step 2

Generate the schema annotations by running:


in the root of your rails project directory and it will inject the schema details for each “table” into the respective “model.rb” file e.g. “user.rb” might contain:

# == Schema Information
# Table name: users
#  id          :integer         not null, primary key
#  screen_name :string(255)
#  name        :string(255)
#  created_at  :datetime
#  updated_at  :datetime

Written by Wes

October 21, 2011 at 5:50 pm

2 Responses

Subscribe to comments with RSS.

  1. Great find, I’m a total RoR noob (but learning) and I was like “WHERE ARE ALLL THE PROPERTIES IN MY MODEL!!!!!”. Then I realized the meta data is stored in db/schema.rb, but this is really more of a script to help generate a schema, not really document it.

    I want to go to a model and see all the fields that comprise it gosh darnit!

    But of course this didn’t work for me initially, I swear getting gems to work together is the hardest thing about rails. I am using the ‘thin’ gem for web server on heroku but if this is included in your local file on windows bundle install will fail. Once I finally commented out the thin gem I could get this installed and run it.



    January 13, 2012 at 2:17 am

  2. I ended up making my gem file like this:

    group :production do
    #postgres gem for deployment on heroku
    gem ‘pg’

    #use thin for webserver on heroku
    gem ‘thin’

    And then run: “bundle install –without production”


    January 13, 2012 at 2:23 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: