0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

こんにちは。アメリカ在住で独学エンジニアを目指している Taira です。

Rails アプリケーションでは、モデルファイルにそのテーブルのカラム情報をコメントとして記載することで、モデルの構造をすばやく把握できるようにする習慣があります。
その作業を自動化してくれるのが annotate 系の gem です。

ただし、従来の annotateRails 8 では依存関係の問題が起きる ため使用できません。
その代替として登場したのが annotaterb です。本記事では annotaterb の属性や導入方法、使い方を解説します。


annotaterb とは

annotaterb は、Rails モデルにスキーマ情報をコメントとして自動追加する gem です。
以下のような情報がモデルファイルの先頭に追加されます:

# == Schema Information
#
# Table name: users
#
#  id         :bigint           not null, primary key
#  name       :string(50)
#  email      :string           not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
class User < ApplicationRecord
  ...
end

属性

  • Rails 8 対応
  • .annotate で設定管理
  • model、migration、routes などにスキーマ情報を追加可
  • 実行時にデータベースをスキャンして情報を取得

導入手順

1. Gemfile に追加

group :development do
  gem 'annotaterb', require: false
end

インストール:

$ bundle install

2. 初期設定

$ rails g annotate_rb:install

使い方

全モデルにスキーマ情報を追加

$ bundle exec annotaterb:models

ルーティングファイルに追加

$ bundle exec annotaterb:routes

まとめ

annotaterb は db のカラムやルーティングを表示するのに最適の gem です
今回は rails8 でやりましたが、ほかのバージョンでも使えるはずなのでぜひ試してみてください

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?