How to use 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
In your Gemfile, you need to add this under the development group
Make sure to run
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.
Another cool feature of Annotate is displaying a set of comments at the top of
This is extremely useful if you find yourself always running the same command to find the route helper for a specific controller/action.
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:
The results are pretty great.