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

Fix defect where aliased attribute methods on abstract classes were not being defined #48991

Merged

Conversation

andrewn617
Copy link
Contributor

@andrewn617 andrewn617 commented Aug 21, 2023

Motivation / Background

This Pull Request has been created because of a defect where no methods for alias attributes defined on an abstract class are being generated.

Detail

a88f47d fixed an issue where subclasses were regenerating aliased attribute methods defined on parent classes. Part of the solution was to call #generate_alias_attributes recursively on a class's superclass to generate all the alias attributes in the inheritance hierarchy. However, the implementation relies on #base_class to determine if we should call #generate_alias_attributes on the superclass. Since all models that inherit from abstract classes are base classes, this means that #generate_alias_attributes will never be called on abstract classes, meaning no method will be generated for any alias attributes defined on them.

To fix this issue, we should always call #generate_alias_attributes on the superclass unless the superclass is ActiveRecord::Base.

Additional information

The docs about how base_class work are somewhat unclear. I plan to open a separate PR to try to improve them.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

…eing not defined

rails/rails@a88f47d fixed an issue where subclasses were regenerating aliased attribute methods defined on parent classes. Part of the solution was to call #generate_alias_attributes recursively on a class's superclass to generate all the alias attributes in the inheritance hierarchy. However, the implementation relies on #base_class to determine if we should call #generate_alias_attributes on the superclass. Since all models that inherit from abstract classes are base classes, this means that #generate_alias_attributes will never be called on abstract classes, meaning no method will be generated for any alias attributes defined on them.

To fix this issue, we should always call #generate_alias_attributes on the superclass unless the superclass is ActiveRecord::Base.
@rafaelfranca rafaelfranca merged commit 6beb348 into rails:main Aug 21, 2023
8 of 9 checks passed
@@ -65,7 +65,7 @@ def eagerly_generate_alias_attribute_methods(_new_name, _old_name) # :nodoc:
end

def generate_alias_attributes # :nodoc:
superclass.generate_alias_attributes unless base_class?
superclass.generate_alias_attributes unless superclass == Base
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @ipc103

Sorry I was the one suggesting to use base_class? in #48913 (comment) but I had wrong understanding of its behavior so superclass compared to AR::Base is the more correct check here, as you initially did!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely missed that as well! Thank you @andrewn617 for the quick fix ❤️

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

4 participants