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

    XMLWordPrintable

    Details

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

      Description

      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.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: