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

Bug in modifier and trackable relation #11

Closed
sarcilav opened this issue Jul 5, 2011 · 7 comments
Closed

Bug in modifier and trackable relation #11

sarcilav opened this issue Jul 5, 2011 · 7 comments
Labels

Comments

@sarcilav
Copy link
Contributor

sarcilav commented Jul 5, 2011

When between the modifier model and trackable model you have more than one relation, when you try to write in the 'modifier' mongoid is returning the wrong relation.
I found that in mongoid/relations/bindings/referenced/in.rb the bind method is making something unexpected, it is returning for the modifier the second relation defined between the models, no the one that is created by mongoid-history in https://github.com/sarcilav/mongoid-history/blob/fixing-bug-when-trackable-modifier-has-more-than-one-relation/lib/mongoid/history/trackable.rb#L36 , I think that the problem is in the modifier you don't have to specify the relation, and it creates a weak relation.
I re-wrote two models in the integration test of mongoid-history to double check that the problem is on mongoid-history https://github.com/sarcilav/mongoid-history/blob/fixing-bug-when-trackable-modifier-has-more-than-one-relation/spec/integration/integration_spec.rb

@aq1018
Copy link
Contributor

aq1018 commented Jul 12, 2011

@scarliav

I used your branch, and rake spec failed miserably. I personally never used habtm in my projects, and don't really know what is going on here.

A brief test shows the following traceback:


NoMethodError: undefined method `map' for #<Post:0x000001010bb358>
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/attributes.rb:162:in `method_missing'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/referenced/many_to_many.rb:147:in `block in initialize'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/proxy.rb:33:in `call'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/proxy.rb:33:in `init'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/referenced/many_to_many.rb:145:in `initialize'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:60:in `new'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:60:in `create_relation'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:26:in `build'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:166:in `block (2 levels) in setter'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/extensions/object/yoda.rb:22:in `do_or_do_not'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/bindings/referenced/in.rb:40:in `bind'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/referenced/in.rb:30:in `bind'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/one.rb:21:in `block in load!'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/one.rb:19:in `tap'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/one.rb:19:in `load!'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:28:in `block in build'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:27:in `tap'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:27:in `build'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/relations/accessors.rb:166:in `block (2 levels) in setter'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/attributes/processing.rb:138:in `block in process_relations'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/attributes/processing.rb:133:in `each_pair'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/attributes/processing.rb:133:in `process_relations'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/attributes/processing.rb:121:in `process_pending'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/attributes/processing.rb:28:in `process'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/document.rb:130:in `initialize'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/persistence.rb:168:in `new'
    from /Users/aqian/.rvm/gems/ruby-1.9.2-p180/gems/mongoid-2.0.2/lib/mongoid/persistence.rb:168:in `create'

@aq1018
Copy link
Contributor

aq1018 commented Jul 12, 2011

If you are able to figure out the cause of it. I'd be glad to change / pull

Thanks!

@sarcilav
Copy link
Contributor Author

About the specs my idea is to show that mongoid-history is falling on that situation :)

@matekb
Copy link
Contributor

matekb commented Jan 12, 2013

Was this ever solved? I am experiencing this with version 0.3.1 and mongoid 3.0.16.

@dblock
Copy link
Collaborator

dblock commented Jan 12, 2013

@matekb Post a repro, ideally in a form of a test for mongoid-history and I can take a look.

matekb added a commit to matekb/mongoid-history that referenced this issue Jan 12, 2013
@matekb
Copy link
Contributor

matekb commented Jan 12, 2013

I have written a test in branch Issue11 of https://github.com/matekb/mongoid-history/ that resembles my setup and problem. Please have a look!

Ohh, just saw that my commit was automatically referenced in the previous message. Nice!

@dblock dblock closed this as completed in add5fab Jan 13, 2013
@dblock
Copy link
Collaborator

dblock commented Jan 13, 2013

So the problem is an ambiguous relationship with :user:

     Failure/Error: model.undo! user
     Mongoid::Errors::AmbiguousRelationship:

       Problem:
         Ambiguous relations :models, :external_model 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 :modifier, Mongoid looked on the class Model for a matching relation, but multiples were found that could potentially match: :models, :external_model.
       Resolution:
         On the :modifier relation on Model you must add an :inverse_of option to specify the exact relationship on User that is the opposite of :modifier.

This is because the model already has a relationship with User, and adding a belongs_to :user without an :inverse_of is ambiguous. I added an :modifier_field_inverse_of option to track_history, so now you can do this:

      track_history   ...
                  :modifier_field => :modifier,
                  :modifier_field_inverse_of => nil, # no inverse modifier relationship

Let me know if that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants