Skip to content

Subclasses should inherit associations from parents - #24719

Closed
maclover7 wants to merge 1 commit into
rails:masterfrom
maclover7:fix-20678
Closed

maclover7 wants to merge 1 commit into
rails:masterfrom
maclover7:fix-20678

Conversation

@maclover7

Copy link
Copy Markdown
Contributor

I can add a test to check on SpecialUser._reflections['comments'], but I wasn't sure if that was digging too deep into Active Record internals 😬

@tallysmartins

tallysmartins commented May 11, 2016 •

Copy link
Copy Markdown

Take my money! xD

@maclover7

Copy link
Copy Markdown
Contributor Author

r? @sgrif

@sgrif

sgrif commented May 11, 2016

Copy link
Copy Markdown
Contributor

Can you add a test which eager loads as well?

Comment thread activerecord/test/schema/schema.rb Outdated

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.

This sounds like something a test is relying on -- otherwise, why would it have been specifically set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It looks like this has been around since roughly 2008 (centralized DB schema + git history gets a bit sketchy after that, looks like could have been around since 2006), but no tests break when I remove this, so... 😬

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.

My worry is that a test is assuming it's there, for the purposes of proving that something works with a null: false column. So removing it wouldn't cause a test to fail, but may cause a test to stop testing what it previously was.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How would you recommend that I check to make sure this isn't happening? 😬 Worst come to worst, I can add another model, and start fresh.

@maclover7

Copy link
Copy Markdown
Contributor Author

updated @sgrif

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.

At a glance, this sounds like too high-level of a place to do this: aren't other places expecting _reflect_on_association to have a complete view of the available associations?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should I push this down the stack to _reflect_on_association? The problem there, is that since this method is included via an AS::Concern, it doesn't know about the current class its dealing with, and thus wouldn't be able to construct the superclass tree, etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes that is probably the level to push this to, but we should have tests
for all the visible effects of this regardless of where it's implemented

On Wed, May 11, 2016, 10:33 PM Jon Moss [email protected] wrote:

In activerecord/lib/active_record/associations.rb
#24719 (comment):

@@ -229,7 +229,16 @@ def association(name) #:nodoc:
association = association_instance_get(name)

   if association.nil?
  •    unless reflection = self.class._reflect_on_association(name)
    
  •    reflection = self.class._reflect_on_association(name)
    

Should I push this down the stack to _reflect_on_association? The problem
there, is that since this method is included via an AS::Concern, it
doesn't know about the current class its dealing with, and thus wouldn't be
able to construct the superclass tree, etc.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/rails/rails/pull/24719/files/e22146a58b93d4a5ea53ef481387738f58bb1fd5#r62957427

@matthewd

Copy link
Copy Markdown
Member

It's worth noting the model knows which classes have inherited from it. So instead of having the subclass hold an almost-but-not-quite-complete list of associations but then still fall back to the parent, maybe we should walk the children at the time the new association is defined.

Otherwise, if we're going to fall back to the parent anyway, maybe the subclass should only be tracking its own associations, and never those defined further up.

marcosronaldo added a commit to marcosronaldo/noosfero that referenced this pull request May 12, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <[email protected]>
Signed-off-by: Marcos Ronaldo <[email protected]>
Signed-off-by: Tallys Martins <[email protected]>
@maclover7

Copy link
Copy Markdown
Contributor Author

@matthewd I think if we rely on the parent class to manage its subclasses, associations could get out of date. An example of this could be that in app/models/, you have a Post model that defines a has_many. Depending on what the order of file loading is for models (this is beginning to get out of my wheelhouse 😄), it is possible that a subclass would never get its reflections updated, with the Post's association.

Not sure if this is a real thing we need to be considering -- if not, I'm happy to redo the implementation if that's a better course (superclass going through and adding associations to its subclasses, when defined)

@maclover7

Copy link
Copy Markdown
Contributor Author

After reviewing the associations code some more, totally on same page as @matthewd -- parents should take responsibility for defining associations on their children. Updated code to reflect this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If anyone has a way of making ar.descendants << ar look prettier, I'm all for it. All we're doing here is creating an array with all of the classes we're going to be adding the new reflection to (ar's descendants and ar itself), to try and stay DRY. ☔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't know how to improve on ar.descendants << ar, but I do have a 😊 reaaally 😊 small nitpick: record var name is usually used for AR instances, maybe switch record -> ar_class?

@brauliobo

Copy link
Copy Markdown

please check the duplicate at #21908

@sgrif

sgrif commented May 16, 2016

Copy link
Copy Markdown
Contributor

@brauliobo Sorry about that, sometimes it's hard to keep things like that from slipping through the cracks. I'm working to get the backlog down so that doesn't happen anymore. @maclover7 Can you credit him in the commit as well after you squash & rebase?

@brauliobo

Copy link
Copy Markdown

no problem @sgrif, thanks for the credit note.

btw, I think the best fix would be to add an option to class_attribute so that it handle sharing data from base class to subclasses if necessary like in this case.

@sgrif

sgrif commented May 16, 2016

Copy link
Copy Markdown
Contributor

I don't think this is something we can generalize as it only applies to
hashes

On Mon, May 16, 2016, 12:15 PM Bráulio Bhavamitra [email protected]
wrote:

no problem @sgrif https://github.com/sgrif, thanks for the credit note.

btw, I think the best fix would be to add an option to class_attribute so
that it handle sharing data from base class to subclasses if necessary like
in this case.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#24719 (comment)

- Fixes #20678 (Subclass misses association defined on parent)

[Jon Moss, Bráulio Bhavamitra]
@maclover7

Copy link
Copy Markdown
Contributor Author

👍 updated PR

tallysmartins pushed a commit to tallysmartins/noosfero that referenced this pull request May 18, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <[email protected]>
Signed-off-by: Marcos Ronaldo <[email protected]>
Signed-off-by: Tallys Martins <[email protected]>
(cherry picked from commit 3b60e7c)
tallysmartins pushed a commit to tallysmartins/noosfero that referenced this pull request May 18, 2016
Temporary fix, waiting for original fix to be merged.

Original author: Jon Moss
Merge Request: rails/rails#24719
Issue: rails/rails#20678

Signed-off-by: Joenio Costa <[email protected]>
Signed-off-by: Marcos Ronaldo <[email protected]>
Signed-off-by: Tallys Martins <[email protected]>

@gmcgibbon gmcgibbon left a comment

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.

This is still an issue in Rails 6 (e925cb4). Please rebase if you're still interested in merging this.

self.table_name = 'pirates'
end

class SpecialPirate2 < SpecialPirate1

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.

@rails-bot

rails-bot Bot commented Dec 18, 2019

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 Dec 18, 2019
@rails-bot rails-bot Bot closed this Dec 25, 2019
@daslicious

Copy link
Copy Markdown

I'm experiencing this issue after upgrading to rails 7

tenderlove pushed a commit that referenced this pull request Oct 31, 2026
`add_reflection` writes a new frozen `_reflections` hash to the class
that defines the association. A subclass that had already defined an
association of its own holds its own copy, so an association added to
the parent afterwards never reaches it: the generated reader exists on
the subclass, but `association` can't find the reflection and raises
`AssociationNotFoundError`. Subclasses that only inherit the hash can
also keep a stale `normalized_reflections` cache.

When a reflection is added, walk the class's descendants, clear their
caches, and add it to each one that has its own copy, unless that
subclass (or a class between it and the parent) defines the same name.

This follows the direction from the review of #24719.

Fix #20678
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.

Subclass misses association defined on parent

8 participants