Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use call_args in the define_proxy_method namespace. #48844

Conversation

nvasilevski
Copy link
Contributor

Substitutes #48843

define_proxy_call accepts call_args as an argument which impacts method body generation but it doesn't use call_args in the namespace generation which leads to the same cached method to be reused even if call_args differ.

It has never been an issue since define_proxy_call was only used to generate Active Record attribute methods and we were always passing the same call_args per method name.

However, since #48533 we are using define_proxy_call to generate alias attribute methods where call_args differ for the same method name which leads to the same cached method being reused in wrong places.

This commit fixes the issue by making sure call_args are being considered when generating the namespace for the method as anything that results in a different body being generated should be accounted when building the cache key

Problem example

Consider the following models:

class Message < ActiveRecord::Base
  alias_attribute :subject, :header
end

class Topic < ActiveRecord::Base
  alias_attribute :subject, :title
end

Since #48533 we expect Rails to generate the following getters for aliases:

class Message < ActiveRecord::Base
  def subject
     attribute(:header)
  end
end

class Topic < ActiveRecord::Base
  def subject
     attribute(:title)
  end
end

Due to Rails using caching mechanism for method generation and the cache key consists of the hardcoded proxy_alias_attribute plus the proxy_target which is "attribute" only one subject getter method will actually be generated and it depends on whichever of the models gets loaded first, the other model will reuse the cached method with a wrong body

`define_proxy_call` accepts `call_args` as an argument which impacts
method body generation but it doesn't use `call_args` in the `namespace`
generation which leads to the same cached method to be reused even if
`call_args` differ.

It has never been an issue since `define_proxy_call` was only used to
generate Active Record attribute methods and we were always passing
the same `call_args` per method name.

However, since rails#48533 we are using
`define_proxy_call` to generate alias attribute methods where `call_args`
differ for the same method name which leads to the same cached method
being reused in wrong places.

This commit fixes the issue by making sure `call_args` are being
considered when generating the `namespace` for the method.
@eileencodes eileencodes merged commit 05d93c7 into rails:main Jul 31, 2023
9 checks passed
@eileencodes eileencodes deleted the use-call-args-for-define-proxy-method-namespace branch July 31, 2023 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants