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

first_or_initialize with polymorphic associations

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      When using first_or_initialize on a model with a polymorphic association the type field is not set.

      class Car
         include Mongoid::Document
          belongs_to :drivable, polymorphic: true
      end
      
      class Driver
         include Mongoid::Document
         has_many :cars, as: :drivable
      end
      
      driver = Driver.create
      Car.where(drivable: driver).first_or_initialize 
      
      #=> #<Car _id: 5587e09d7661671716010000, drivable_type: nil, drivable_id: BSON::ObjectId('5587e0997661671716000000')>
      
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            sdbondi sdbondi
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: