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?

【Rails 8.1】関連付けの非推奨化が追加された

Posted at

はじめに

Rails8.1で、ActiveRecordの関連付けに対して非推奨フラグを付与できる新機能が実装された

この機能は以下PRで実装された

これにより、今後は廃止予定だから使って欲しくない関連付けの使用箇所を検出しやすくなる

deprecatedについて

Rails8.1では、以下のようにdeprecatedとしてマークできる

class Author < ApplicationRecord
  has_many :books, deprecated: true
end

このように設定すると、以下のようにbooksへアクセスしたときに警告が表示される
スクリーンショット 2025-12-09 18.49.03.png

また、直接的な参照でなくても、preloadなどの間接的な参照をしている部分で警告が表示される
スクリーンショット 2025-12-09 18.49.49.png

モード

非推奨化には、以下の3種類のモードがサポートされている

モード 動作
:warn 警告としてログに出力(デフォルト)
:raise 利用した時点で例外発生
:notify Active Support notificationsとして発行

また、スタックトレースの可否も設定できる

このモードは、以下のようにconfigで変更することができる

application.rb
config.active_record.deprecated_associations_options = { mode: :notify, backtrace: true }

参考

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?