Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2792

Support a way not to coerce polymorphic relationships into the exact type of the relationship

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Consider the following (failing) example. Mongoid will coerce the SpecialPartner relationship into having property_type=SpecialPartner. This makes it hard to locate all objects of AccessControl type that belong to a Partner. We used to use the exact code below and overwrite property_type with Partner, which is no longer possible on HEAD.

       ruby
      require "spec_helper"
      
      class Partner
        include Mongoid::Document
        has_one :access_control, as: :property
      end
      
      class SpecialPartner < Partner
      
      end
      
      class AccessControl
        include Mongoid::Document
        belongs_to :property, polymorphic: true
      end
      
      describe Mongoid do
      
        it "issue" do
      
          partner = SpecialPartner.create!
          acl = AccessControl.find_or_create_by({ property_type: 'Partner', property_id: partner.id })
          acl.property_type.should == 'Partner'
      
          acl.reload
          acl.property_type.should == 'Partner'
      
          acl.reload
          acl.property.should == partner
          acl.property_type.should == 'Partner'
      
        end
      
      end
      

      TBH I am 50/50 on whether this is a good idea. There're plenty of workarounds, such as storing something else on the model like parent_type, but I thought I'd bring it up.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dblock Daniel Doubrovkine
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: