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-5149 Skip inverse detection when inverse_of: nil is set #5027

Merged
merged 2 commits into from Aug 23, 2021

Conversation

simonc
Copy link
Contributor

@simonc simonc commented Aug 11, 2021

When an association is defined with the inverse_of: nil option, I believe its inverse should not be looked up in any case.

This PR fixes that by skipping the lookup when the option is explicitly defined and set to nil but will still let the lookup happen if the option was not specified and is thus nil.

I didn't push as to create a inverse_of_set? method in the Options module as it would not be defined for any other option but I suppose it could make sense.

Why this PR

We actually got a bug while migrating to mongoid 7 where we have the kind of relationship I defined in the spec and due to the way inverse are looked up, trying to get the inverse of previous_academy here would return the inverse of current_academy and that would in the end cause this error:

ActiveModel::MissingAttributeError:
  Missing attribute: 'current_academy_id'

Which doesn't really make sense.

Copy link
Contributor

@p-mongo p-mongo left a comment

Choose a reason for hiding this comment

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

Hi @simonc , thank you for the PR.

Can you please create your own test case instead of adding your test code to an existing test case. Your issue is with the inverse being explicitly not set, therefore I would expect the test to reflect that in its description (the test you modified has to do with projections).

@@ -36,6 +44,9 @@ class HmmSchool
class HmmStudent
include Mongoid::Document

belongs_to :current_academy, class_name: 'HmmAcademy', inverse_of: :students, optional: true
belongs_to :previous_academy, class_name: 'HmmAcademy', inverse_of: nil, optional: true
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these should also go into a new model, unless there is something else from HmmStudent that your test would need.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can create a dedicated model for this yes.

@simonc
Copy link
Contributor Author

simonc commented Aug 12, 2021

Hi @p-mongo. Actually it's somewhat related and that's why I've added it to the existing test as it's the very reason I faced the issue in the first place.

If the projection lists fields for an association that can conflict with another one due to the bug, you get the MissingAttributeError. Without projection you don't get an error as the inverse will be wrongly defined but not cause an issue.

That said, I can still create a separate spec for the specific case.

@simonc
Copy link
Contributor Author

simonc commented Aug 12, 2021

@p-mongo I updated the PR to have its own models and spec.

@johnnyshields
Copy link
Contributor

👍 looks good to me

@p-mongo p-mongo changed the title Skipping inverse relation detection when inverse_of: nil is set MONGOID-5149 Skipping inverse relation detection when inverse_of: nil is set Aug 20, 2021
@p-mongo p-mongo changed the title MONGOID-5149 Skipping inverse relation detection when inverse_of: nil is set MONGOID-5149 Skip inverse detection when inverse_of: nil is set Aug 20, 2021
@p-mongo
Copy link
Contributor

p-mongo commented Aug 20, 2021

Thank you very much @simonc , this looks very clear now.

@p-mongo p-mongo requested a review from comandeo August 20, 2021 02:22
@p-mongo p-mongo merged commit f273227 into mongodb:master Aug 23, 2021
p-mongo pushed a commit that referenced this pull request Aug 23, 2021
* Skipping inverse relation detection when inverse_of: nil is set

* Extracting a specific spec for the matter at hand
p-mongo pushed a commit that referenced this pull request Aug 23, 2021
* Skipping inverse relation detection when inverse_of: nil is set

* Extracting a specific spec for the matter at hand
@simonc simonc deleted the skip-inverse-of-when-set-to-nil branch December 7, 2021 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants