[Feature Proposal] find_by and where should cache association

Hello, everyone. I propose fetched model using find_by or where by associated model should cache their associations.

class User < ApplicationRecord
  has_one :user_profile

class UserProfile < ApplicationRecord
  belongs_to :user
user = User.first # Run SQL
user_profile = UserProfile.find_by(user: user) # Run SQL
user_profile.user # Run SQL, but I think this SQL should not run.

I think user_profile = UserProfile.find_by(user: user) should cache user association and user_profile.user should not run SQL.

See: `find_by` and `where` should cache association · Issue #50083 · rails/rails · GitHub

1 Like