Post

TIL: The Annotate Gem

Today, while watching a video by CJ Avilla, he used a neat little gem that describes the schema of the models in his project.

At 8:32 (video is cued up below) he installs the Annotate gem and then runs an annotate command.

CJ uses the gem so that he could quickly reference the fields in the model without having to search the schema.rb file.

Testing it out

Add the gem to your project with bundle add annotate and wait for the gem to be installed by Bundler.

Next, run bundle exec annotate --models, command and let the gem do its magic! Each model file gets updated with a section of comments at the top of the file that describes the schema of the model.

Below is an example from a test project I have in my environment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
app > models > post.rb

# == Schema Information
#
# Table name: posts
#
#  id         :integer          not null, primary key
#  title      :string
#  content    :text
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
class Post < ApplicationRecord
  has_one_attached :avatar
end

Conclusion

The Annotate gem is a neat addition to projects that annotates the schema of each model file in your project. At a glance, you can open one of your model files and reference the schema.

It’s a powerful little gem with some neat features that aren’t covered here. Go check out their GitHub and read the readme file for all of the available options.

This post is licensed under CC BY 4.0 by the author.