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

MONGOID-5790 MONGOID-5791 Fix error caused by loading a referenced class prematurely #5839

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

jamis
Copy link
Contributor

@jamis jamis commented Jul 12, 2024

The problem manifested when a class declared an association on a class that hadn't yet been loaded; when the touchable module began adding its functionality, there were a few lines that tried to load the referenced class, e.g.:

class Child
  include Mongoid::Document
  embedded_in :parent
end

class Parent
  include Mongoid::Document
  embeds_many :children
end

This would result in a NameError because the embedded_in association could not find the Parent class at load-time.

I started by checking to see if we could just raise a more informative error in this case, as a stop-gap before spending more time later to actually fix the issue. Imagine my joy when I realized that the entire problem was caused by a few lines of dead-code! By simply deleting the unused code, the problem went away. 🎉

Edit: a bit more explanation here, after I went through and triple checked the logic. An Association has a klass, which is the class that the association references directly. In the above example, the Child#parent association would have Parent as the klass. An Association also has an inverse_class, which is the class that the association belongs to. Again, to use the Child#parent association as an example, this would be the Child class.

The reason everything "just works", even when the klass references haven't yet been defined, is because the touch behaviors are added to the inverse_class, and not the klass. That is to say, when the Mongoid::Touchable model works its magic in an embedded_in association, it adds the new methods to Child, and not Parent. Ditto for any of the other associations. Thus, it doesn't matter (at load time) whether the klass references exist yet or not, because all we care about are the inverse_class references, which are guaranteed to exist.

…ass prematurely

It's such a pleasure to delete dead code...and even more so when the
dead-code is responsible for a bug, simply by existing!
Copy link
Contributor

@comandeo-mongo comandeo-mongo left a comment

Choose a reason for hiding this comment

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

🥇

@jamis jamis merged commit d2fcc26 into mongodb:master Jul 15, 2024
58 checks passed
@jamis jamis deleted the 5790-error-when-embedded-is-not-loaded branch July 15, 2024 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants