How to use the Annotate Gem in Rails 6

Using the Annotate Gem in Rails 6

Get a summary of the Active Record schema at the top or bottom of relevant files

The annotate gem will add a comment to the top of some selected files describing the database schema relevant for those files — including field names, index configuration and foreign keys.

It will update these comments automatically whenever you run rails db:migrate.


In your Gemfile, you need to add this under the development group

  group :development do
    gem 'annotate'

Make sure to run bundle afterwards.


  bundle exec rails g annotate:install

This will generate a rake task in lib/tasks/auto_annotate_models.rake with the default configuration.

As a personal preference, I always disable annotating the controllers (and controller specs) but keep the annotations for FactoryBot’s factories and models.

This is the configuration I use.

Route maps

Another cool feature of Annotate is displaying a set of comments at the top of config/routes.rb.

This is extremely useful if you find yourself always running the same command to find the route helper for a specific controller/action.

  bundle exec rails routes | grep user_password

To set it up, you can manually trigger the annotations using: bundle exec rails annotate_routes

To make it automatically annotate the routes, change lib/tasks/auto_annotate_models.rake and update:

    'routes' => 'true', # was false
    # ...

The results are pretty great.

Annotating Routes in Rails 6