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::Errors::AmbiguousRelationship error #18

Open
ptrikutam opened this issue Nov 3, 2015 · 2 comments
Open

Mongoid::Errors::AmbiguousRelationship error #18

ptrikutam opened this issue Nov 3, 2015 · 2 comments

Comments

@ptrikutam
Copy link

Not sure if this is an issue with

class User
  include Trackable
  has_many :given_gifts, class_name: 'Gift', inverse_of: :giver
  has_many :received_gifts, class_name: 'Gift', inverse_of: :recipient
end

class Gift
  include Trackable
  belongs_to :giver, class_name: '::User', inverse_of: :given_gifts, index: true
  belongs_to :recipient, class_name: '::User', inverse_of: :received_gifts, index: true
end

This used to work just fine with Mongoid 3 / mongoid-audit 0.1.7, but once I upgraded to Mongoid 5 / mongoid-audit 1.1.0, I'm getting this error:

Mongoid::Errors::AmbiguousRelationship:
message:
Ambiguous relations :given_gifts, :received_gifts defined on User.
summary:
When Mongoid attempts to set an inverse document of a relation in memory, it needs to know which relation it belongs to. When setting :updater, Mongoid looked on the class Gift for a matching relation, but multiples were found that could potentially match: :given_gifts, :received_gifts.
resolution:
On the :updater relation on Gift you must add an :inverse_of option to specify the exact relationship on User that is the opposite of :updater.

I don't have an :updater relation defined in my model, but I do have include Trackable in my Gift and User model.

I'm not quite sure how to properly define the inverse_of option on the :updater relation, since it's buried away somewhere else. Any idea what I could do to address this?

@ptrikutam ptrikutam changed the title AmbiguousRelationship erro AmbiguousRelationship error Nov 3, 2015
@ptrikutam ptrikutam changed the title AmbiguousRelationship error Mongoid::Errors::AmbiguousRelationship error Nov 3, 2015
@Shwetakale
Copy link

@ptrikutam This is because when you track updater in gifts it is default associated with User.

Effectively

belongs_to :updater, class_name: 'User'

but you don't have inverse relationship from User . User is associated with gifts but with given_gifts and received_gifts.

Here if you don't require association from User set inverse_of: nil to ```gifts as follow.

track_history  :modifier_field_inverse_of => :nil, # adds an ":inverse_of" option to the "belongs_to :modifier" relation, default is not set

If you want to find out gifts updated by user then you need to set inverse_of in gifts

track_history  modifier_field_inverse_of: :updated_gifts

And in User model

 has_many :updated_gifts, class_name: 'Gift', inverse_of: :updater

Hope this helps.

@ptrikutam
Copy link
Author

@Shwetakale This is really great, thanks. Very helpful.

Right now I'm just adding include Trackable to the Gift model and the User model.

Where would I add the snippet:

track_history  modifier_field_inverse_of: :updated_gifts

Should I just add that in the User model below include Trackable?

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

No branches or pull requests

2 participants