Using embedded associations in conjunction with only causes MissingAttributeError

XMLWordPrintableJSON

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

      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
              Reporter:
              Tapio Saarinen
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated: