Skip to content

Ensure attributes are always inherited - #39467

Closed
jonathanhefner wants to merge 1 commit into
rails:masterfrom
jonathanhefner:ensure-attributes-inherited
Closed

jonathanhefner wants to merge 1 commit into
rails:masterfrom
jonathanhefner:ensure-attributes-inherited

Conversation

@jonathanhefner

Copy link
Copy Markdown
Member

Prior to this commit, attributes were not inherited by a subclass if they were added to the parent class after the subclass had added its own attributes. This was due to the way attribute definitions were stored in an append-only class_attribute. This commit changes the way attribute definitions are stored to ensure they are always inherited.


Splitting this off from #38871.

Prior to this commit, attributes were not inherited by a subclass if
they were added to the parent class after the subclass had added its own
attributes.  This was due to the way attribute definitions were stored
in an append-only `class_attribute`.  This commit changes the way
attribute definitions are stored to ensure they are always inherited.
super
attributes_to_define_after_schema_loads.each do |name, (type, options)|

deferred = ancestors.map { |klass| klass.try(:deferred_attribute_definitions) }.compact.reduce(&:reverse_merge)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find this line a bit hard to grok, what about merging more imperatively:

Suggested change
deferred = ancestors.map { |klass| klass.try(:deferred_attribute_definitions) }.compact.reduce(&:reverse_merge)
deferred = ancestors.each_with_object({}) do |mod, memo|
memo.reverse_merge!(mod.deferred_attribute_definitions) if mod.respond_to?(:deferred_attribute_definitions)
end

Or walking up the superclass chain:

diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index 962e15c1e8..88b250505b 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -240,17 +240,24 @@ def define_attribute(

       def load_schema! # :nodoc:
         super
+        define_deferred_attributes
+      end

-        deferred = ancestors.map { |klass| klass.try(:deferred_attribute_definitions) }.compact.reduce(&:reverse_merge)
+      def define_deferred_attributes(klass: self, defined: Set.new) # :nodoc:
+        deferred_attribute_definitions.each do |name, (type, options)|
+          next if defined.include?(name)

-        deferred.each do |name, (type, options)|
           if type.is_a?(Symbol)
-            adapter_name = ActiveRecord::Type.adapter_name_from(self)
+            adapter_name = ActiveRecord::Type.adapter_name_from(klass)
             type = ActiveRecord::Type.lookup(type, **options.except(:default), adapter: adapter_name)
           end

-          define_attribute(name, type, **options.slice(:default))
+          klass.define_attribute(name, type, **options.slice(:default))
+
+          defined.add(name)
         end
+
+        superclass.try(:define_deferred_attributes, klass: klass, defined: defined)
       end

       private

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we use try here, we should require the core extension (active_support/core_ext/object/try).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be a matter of preference though, I'd not prefer to change both setter side and using side (and drop exisiting attributes_to_define_after_schema_loads and introduce new one are too), unless it is obviously easier to understand.

How about a way to address it only in setter side (in attribute) like as #39473?

@eugeneius

Copy link
Copy Markdown
Member

Splitting this off from #38871.

Thanks, I tried to review #38871 a few times but kept bouncing off it 😅

kamipo added a commit to kamipo/rails that referenced this pull request May 29, 2020
This is a common `class_attribute` implementation issue (e.g. rails#39467,
rails#39468, rails#38871).

For now, we should take care of the change propagation by themselves if
we want to ensure the propagation after the `class_attribute` are
accessed in subclasses.

Fixes rails#20678.
jonathanhefner added a commit to jonathanhefner/rails that referenced this pull request Jun 14, 2020
@rails-bot

rails-bot Bot commented Aug 27, 2020

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot Bot added the stale label Aug 27, 2020
@jonathanhefner

Copy link
Copy Markdown
Member Author

Closing in favor of #39487.

@bf4 bf4 mentioned this pull request Oct 7, 2022
8 of 9 tasks
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.

3 participants