Understanding Rails

Understanding Rails · Season 3: Active Record in depth

Deprecated associations

Episode 26 · deep dive · about 3 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

What changed

  • Version 2, November 20, 2026: Dropped backtrace: true from the raise example, where it does nothing, and a WARN prefix the log doesn't print.

Rails 8.1's way to retire an association without breaking callers.

Free to watch. If it helped, support the series.

Transcript

0:00 Deprecated associations

Episode twenty six. Deprecated associations. New in Rails eight point one. When an association has to go, the hard part is finding everyone who still uses it. Today: how Rails finds them for you.

0:18 Finding every caller

Notes have replies: a has many that points back at notes. Say we're replacing replies with comments. Delete the association, and anything that still calls it breaks, in a view, a job, or a script you forgot. Searching the code for the word replies finds some callers, but not the ones that build the name, or reach it through includes.

0:40 deprecated true

So mark it instead. Deprecated true, on the has many. The association still works exactly as before. It just reports every use.

0:50 The warning

Call note dot replies, and the log says: the association note replies is deprecated, the method replies was invoked. And in brackets, the file and the line that called it: lib replies report dot r b, line three.

1:06 includes and joins

It catches more than method calls. Includes replies is reported as a query to preload records. Joins replies, as a query to join its table. Creating a reply through it is reported too. Each of these is a caller that a search for the method name could miss.

1:24 Three modes

Warning in the log is the default mode. There are two more, set in each environment's config with deprecated associations options.

1:33 raise

Raise turns every use into an error: deprecated association error, with the same message. Use it in the test environment, and any test that still touches replies fails, pointing at the line.

1:47 notify

Notify sends an Active Support notification instead. Subscribe to deprecated association dot active record, and each use arrives with its message, the location, and the association itself. In production, send those to your error tracker, and learn which callers you missed without bothering a single user.

2:08 Retire it in steps

Then retire it in steps. Mark it deprecated. Raise in tests, and fix what fails. Watch production until the reports stop. Then delete the association, knowing nothing still calls it.

2:22 Next

That ends season three. Next, season four begins with routing in depth.

Check your understanding

  1. You add deprecated: true to has_many :replies and change nothing else. What does note.replies do?

    • Raises an error
    • Returns the replies, and writes a warning to the log
    • Returns an empty list
    • Returns the replies, with no warning until you set a mode
    Show answer

    Returns the replies, and writes a warning to the log. The association still works. The default mode is warn, which logs each use with the line that made it. Source: ActiveRecord::Associations::ClassMethods: deprecated associations (Rails API 8.1).

  2. replies is deprecated. Which of these does Rails report?

    • Only calls to note.replies
    • Only calls in the test environment
    • Calls to note.replies, and running queries that use it, like includes(:replies) or joins(:replies)
    Show answer

    Calls to note.replies, and running queries that use it, like includes(:replies) or joins(:replies). Running a query that refers to the association counts as a use, so Rails finds callers a search for the method name could miss. Source: ActiveRecord::Associations::ClassMethods: deprecated associations (Rails API 8.1).

  3. You set deprecated_associations_options to { mode: :raise } in the test environment. What does a test that calls note.replies get?

    • ActiveRecord::DeprecatedAssociationError
    • A warning in the test log
    • A skipped test
    • NoMethodError
    Show answer

    ActiveRecord::DeprecatedAssociationError. In raise mode every use raises a DeprecatedAssociationError with the same message, so the test fails at that line. Source: Configuring Rails Applications: config.active_record.deprecated_associations_options (Rails Guides 8.1).

  4. In notify mode, which event do you subscribe to, to send each use to your error tracker?

    • deprecation.rails
    • sql.active_record
    • deprecated_association.active_record
    • association.deprecated
    Show answer

    deprecated_association.active_record. Notify mode publishes deprecated_association.active_record, with the message, the location and the association's reflection. Source: Active Support Instrumentation: deprecated_association.active_record (Rails Guides 8.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.

Comments

Sign in to comment or sign up.