One-to-many polymorphic reference problem when there is another reference on the parent side

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • 3.0.5
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Hi there,

      First of all, I would like to thank you all for making Mongoid, it is a really well executed project. We had an issue when upgrading from Mongoid 2.4.12 to 3.0.4. In our project we have an entity that references another entity multiple times, one for each concern it represents, like the one below:

      # Users can act both as resource owners and resource collaborators
      class User
        include Mongoid::Document
        include Owner
        include Collaborator
      
      end
      

      The concerns are represented as follows:

      module Collaborator
        extend ActiveSupport::Concern
      
        included do
          has_many :tasks, :as => :collaborator, :dependent => :destroy
          has_many :collaborator_resources, :as => :collaborator, :class_name => 'SharedResource'
        end
      
      end
      
      module Owner
        extend ActiveSupport::Concern
      
        included do
          has_many :permissions, :as => :owner, :dependent => :destroy
          has_many :owner_resources, :as => :owner, :class_name => 'SharedResource'
        end
      
      end
      

      Each concern associates the parent again to the following entities:

      class Task
        include Mongoid::Document
      
        belongs_to :collaborator, :polymorphic => true
        validates :collaborator, :presence => true
      
      end
      
      class Permission
        include Mongoid::Document
      
        belongs_to :owner, :polymorphic => true
        validates :owner, :presence => true
      
      end
      

      The problem is, Mongoid 3.x is raising the following error in this scenario:

      > Mongoid::Errors::InvalidSetPolymorphicRelation:
      >
      > Problem:
      > The owner attribute can't be set to an instance of User as User has multiple relations referencing SharedResource as owner.
      > Summary:
      > If the parent class of a polymorphic relation has multiple definitions for the same relation, the values must be set from the parent side and not the child side since Mongoid cannot determine from the child side which relation to go in.
      > Resolution:
      > Set the values from the parent, or redefine the relation with only a single definition in the parent.

      The same relations work fine in Mongoid 2.4.x, also I can't see where the above example might be wrong by definition. I wonder if there is an issue with Mongoid 3.x and multiple polymorphic associations.

      To better illustrate the issue, I created two sample repositories with this example and specs to check this behavior, one for each branch of Mongoid. The samples are on:

      erichmachado/issue-sample-mongoid-3
      erichmachado/issue-sample-mongoid-2

      I really appreciate your help and feedback on this. Thank you,
      Erich

            Assignee:
            Unassigned
            Reporter:
            Erich Soares Machado
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: