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

Using embedded associations in conjunction with only causes MissingAttributeError

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 8.1.5
    • Component/s: Associations
    • None
    • Ruby Drivers

      If you use `.only` when fetching a model that embeds other models, then use the relation metadata to try to traverse back up from the embedded models, you get a missing attribute error

      This issue is not present in v6 or v7

      class Car
        include Mongoid::Document
      
        embeds_one :steering_wheel
      end
      
      class SteeringWheel
        include Mongoid::Document
      
        embedded_in :car
      end
      
      Car.collection.drop
      id = BSON::ObjectId.new
      Car.collection.insert_one "_id" => id, "steering_wheel" => \{ "_id" => BSON::ObjectId.new }
      car = Car.where(id: id).only("_id", "steering_wheel._id").first
      # 7.5 - works
      # 8.1 - raises ActiveModel::MissingAttributeError
      pp car.steering_wheel.car
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            tapio@ecoportal.co.nz Tapio Saarinen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: