-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Unknown
-
Affects Version/s: 9.1.0, 9.1.1
-
Component/s: Associations
-
None
-
None
-
Fully Compatible
-
Ruby Drivers
-
Not Needed
-
-
None
-
None
-
None
-
None
-
None
-
None
Criteria#eager_load writes the preloaded documents into a field named after the association (for example device), and hydration keeps that field in the document's attributes. Any write of the whole document, such as upsert or reassigning an embedded collection, then saves that copy, overwriting any data already stored under that name. Because the getter for a referenced association reads a stored field with the association's name instead of querying, later loads keep returning the stale copy. A fix is proposed in https://github.com/mongodb/mongoid/pull/6202.
class Device include Mongoid::Document end class Port include Mongoid::Document belongs_to :device end device = Device.create! port = Port.create!(device: device) Port.eager_load(:device).first.upsert Port.collection.find(_id: port.id).first # => { "_id" => ..., "device_id" => ..., "device" => [ { "_id" => ... } ] }