Need help with annotate_models?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

3.8K Stars 482 Forks Other 1.0K Commits 88 Opened issues


Annotate Rails classes with schema and routes info

Services available


Need anything else?

Contributors list

Annotate (aka AnnotateModels)

Gem Version Downloads count Build status CI Status Coveralls Maintenability Inline docs

Add a comment summarizing the current schema to the top or bottom of each of your...

  • ActiveRecord models
  • Fixture files
  • Tests and Specs
  • Object Daddy exemplars
  • Machinist blueprints
  • Fabrication fabricators
  • Thoughtbot's factorybot factories, i.e. the `(spec|test)/factories/factory.rb` files
  • routes.rb
    file (for Rails projects)

The schema comment looks like this:

# == Schema Info
# Table name: line_items
#  id                  :integer(11)    not null, primary key
#  quantity            :integer(11)    not null
#  product_id          :integer(11)    not null
#  unit_price          :float
#  order_id            :integer(11)

class LineItem < ActiveRecord::Base belongs_to :product . . .

It also annotates geometrical columns,

type and
, when using
# == Schema Info
# Table name: trips
#  local           :geometry        point, 4326
#  path            :geometry        line_string, 4326

Also, if you pass the

option, it'll annotate
with the output of
rake routes

Upgrading to 3.X and annotate models not working?

In versions 2.7.X the annotate gem defaulted to annotating models if no arguments were passed in. The annotate gem by default would not allow for routes and models to be annotated together. A change was added in #647. You can read more here.

There are a few ways of fixing this:

  • If using CLI explicitly pass in models flag using


a) Running

rails g annotate:install
will overwrite your defaults with the annotating
option set to

b) In

add the
key-value option:
      'models'                      => 'true',


Into Gemfile from

group :development do
  gem 'annotate'

Into Gemfile from Github:

group :development do
  gem 'annotate', git: ''

Into environment gems from

gem install annotate

Into environment gems from Github checkout:

git clone annotate_models
cd annotate_models
rake gem
gem install dist/annotate-*.gem


(If you used the Gemfile install, prefix the below commands with

bundle exec

Usage in Rails

To annotate all your models, tests, fixtures, and factories:

cd /path/to/app

To annotate just your models, tests, and factories:

annotate --models --exclude fixtures

To annotate just your models:

annotate --models

To annotate routes.rb:

annotate --routes

To remove model/test/fixture/factory/serializer annotations:

annotate --delete

To remove routes.rb annotations:

annotate --routes --delete

To automatically annotate every time you run

, either run
rails g annotate:install
or add
to your

See the configuration in Rails section for more info.

Usage Outside of Rails

Everything above applies, except that

is not meaningful, and you will probably need to explicitly set one or more
option(s), and/or one or more
options to inform
about the structure of your project and help it bootstrap and load the relevant code.


If you want to always skip annotations on a particular model, add this string anywhere in the file:

# -*- SkipSchemaAnnotations

Configuration in Rails

To generate a configuration file (in the form of a

file), to set default options:
rails g annotate:install

Edit this file to control things like output format, where annotations are added (top or bottom of file), and in which artifacts.

The generated rakefile

also contains
. This adds a few rake tasks which duplicate command-line functionality:
rake annotate_models                          # Add schema information (as comments) to model and fixture files
rake annotate_routes                          # Adds the route map to routes.rb
rake remove_annotation                        # Remove schema information from model and fixture files

By default, once you've generated a configuration file, annotate will be executed whenever you run

rake db:migrate
(but only in development mode). If you want to disable this behavior permanently, edit the
file and change:
    'skip_on_db_migrate'   => 'false',


    'skip_on_db_migrate'   => 'true',

If you want to run

rake db:migrate
as a one-off without running annotate, you can do so with a simple environment variable, instead of editing the


Usage: annotate [options] [model_file]*
        --additional-file-patterns   Additional file paths or globs to annotate, separated by commas (e.g. `/foo/bar/%model_name%/*.rb,/baz/%model_name%.rb`)
    -d, --delete                     Remove annotations from all model files or the routes.rb file
    -p [before|top|after|bottom],    Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)
        --pc, --position-in-class [before|top|after|bottom]
                                     Place the annotations at the top (before) or the bottom (after) of the model file
        --pf, --position-in-factory [before|top|after|bottom]
                                     Place the annotations at the top (before) or the bottom (after) of any factory files
        --px, --position-in-fixture [before|top|after|bottom]
                                     Place the annotations at the top (before) or the bottom (after) of any fixture files
        --pt, --position-in-test [before|top|after|bottom]
                                     Place the annotations at the top (before) or the bottom (after) of any test files
        --pr, --position-in-routes [before|top|after|bottom]
                                     Place the annotations at the top (before) or the bottom (after) of the routes.rb file
        --ps, --position-in-serializer [before|top|after|bottom]
                                     Place the annotations at the top (before) or the bottom (after) of the serializer files
        --w, --wrapper STR           Wrap annotation with the text passed as parameter.
                                     If --w option is used, the same text will be used as opening and closing
        --wo, --wrapper-open STR     Annotation wrapper opening.
        --wc, --wrapper-close STR    Annotation wrapper closing
    -r, --routes                     Annotate routes.rb with the output of 'rake routes'
        --models                     Annotate ActiveRecord models
    -a, --active-admin               Annotate active_admin models
    -v, --version                    Show the current version of this gem
    -m, --show-migration             Include the migration version number in the annotation
    -k, --show-foreign-keys          List the table's foreign key constraints in the annotation
        --ck, --complete-foreign-keys
                                     Complete foreign key names in the annotation
    -i, --show-indexes               List the table's database indexes in the annotation
    -s, --simple-indexes             Concat the column's related indexes in the annotation
        --model-dir dir              Annotate model files stored in dir rather than app/models, separate multiple dirs with commas
        --root-dir dir               Annotate files stored within root dir projects, separate multiple dirs with commas
        --ignore-model-subdirects    Ignore subdirectories of the models directory
        --sort                       Sort columns alphabetically, rather than in creation order
        --classified-sort            Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns
    -R, --require path               Additional file to require before loading models, may be used multiple times
    -e [tests,fixtures,factories,serializers],
        --exclude                    Do not annotate fixtures, test files, factories, and/or serializers
    -f [bare|rdoc|yard|markdown],    Render Schema Infomation as plain/RDoc/YARD/Markdown
        --force                      Force new annotations even if there are no changes.
        --frozen                     Do not allow to change annotations. Exits non-zero if there are going to be changes to files.
        --timestamp                  Include timestamp in (routes) annotation
        --trace                      If unable to annotate a file, print the full stack trace, not just the exception message.
    -I, --ignore-columns REGEX       don't annotate columns that match a given REGEX (e.g. `annotate -I '^(id|updated_at|created_at)'`)
        --ignore-routes REGEX        don't annotate routes that match a given REGEX (e.g. `annotate -I '(mobile|resque|pghero)'`)_
        --hide-limit-column-types VALUES
                                     don't show limit for given column types, separated by commas (e.g. `integer,boolean,text`)
        --hide-default-column-types VALUES
                                     don't show default for given column types, separated by commas (e.g. `json,jsonb,hstore`)
        --ignore-unknown-models      don't display warnings for bad model files
        --with-comment               include database comments in model annotations





Provide additional paths for the gem to annotate. These paths can include globs. It is recommended to use absolute paths. Here are some examples:

  • /app/lib/decorates/%MODEL_NAME%/*.rb
  • /app/lib/forms/%PLURALIZED_MODEL_NAME%/**/*.rb
  • /app/lib/forms/%TABLE_NAME%/*.rb

The appropriate model will be inferred using the

syntax, annotating any matching files. It works with existing filename resolutions (options for which can be found in the
method of

When using in a Rails config, you can use the following:



By default, columns will be sorted in database order (i.e. the order in which migrations were run).

If you prefer to sort alphabetically so that the results of annotation are consistent regardless of what order migrations are executed in, use



The format produced is actually MultiMarkdown, making use of the syntax extension for tables. It's recommended you use

as your parser if you want to use this format. If you're using
to generate documentation, specify a format of markdown with
as the provider by adding this to your
--markup markdown
--markup-provider kramdown

Be sure to add this to your

as well:
gem 'kramdown', groups => [:development], require => false


Don't add text after an automatically-created comment block. This tool will blow away the initial/final comment block in your models if it looks like it was previously added by this gem.

Be sure to check the changes that this tool makes! If you are using Git, you may simply check your project's status after running

$ git status

If you are not using a VCS (like Git, Subversion or similar), please tread extra carefully, and consider using one.


  • Factory Bot:
  • Object Daddy:
  • Machinist:
  • Fabrication:
  • SpatialAdapter:
  • PostgisAdapter:
  • PostGISAdapter:


Released under the same license as Ruby. No Support. No Warranty.



We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.