Proposal: Safe deprecate associations

Description

I’d like to propose a feature that allows developers to mark Active Record associations as deprecated, triggering warnings whenever they are used.

Motivation

Currently, Rails provides ways to deprecate methods, but there is no built-in mechanism to deprecate associations. This would be useful for cases where:

  • A model’s association is being phased out in favor of another relationship.
  • Developers want to warn against legacy associations without immediately removing them.
  • Large applications need a smoother migration path when refactoring models.

Proposed Solution

Introduce a association_deprecated: true option to association declarations, which would log deprecation warnings when the association is used.

Example:

class User < ApplicationRecord
  has_many :posts, association_deprecated: true
end

user = User.first  
user.posts  # => Logs a deprecation warning  
User.includes(:posts)  # => Logs a deprecation warning  
User.joins(:posts)  # => Logs a deprecation warning  
User.eager_load(:posts) # => Logs a deprecation warning
SomethingElse.joins(user: :posts) # => Logs a deprecation warning

The deprecation warning would be logged through Application.deprecator.warn, ensuring it follows Rails’ standard deprecation practices.

I have taken the first step of forking rails and writing code that should support this. But before i take this any further I wanted to get a feel for if this feature is something that aligns with Rails.

You can find my preliminary code changes here: GitHub - Tambiebarango/rails: Ruby on Rails

Thank you Theodore