-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 5.0.1, 5.0.2
-
Component/s: None
-
None
-
Environment:ruby 2.2.2
Looks like embedded documents always do autosave but that cause some unexpected bug, if you use several embedded collections.
Look at my comments on unexpected behavior of mongoid
require 'mongoid' class Product include Mongoid::Document field :title, type: String embeds_many :specs, class_name: 'Product::Spec' embeds_many :photos, class_name: 'Product::Photo' end class Product::Spec include Mongoid::Document field :name, type: String field :value embedded_in :product end class Product::Photo include Mongoid::Document field :image, type: String embedded_in :product end Mongoid.load! "mongoid.yml", :development Mongo::Logger.logger.level = 0 Product.destroy_all product = Product.new(title: "BMW X5") product.specs = [{name: "make", value: "BMW"}, {name: "model", value: "X5"}] product.photos = [{image: "http://example.com/"}] product.save #D, [2015-12-29T12:46:11.473895 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.insert | STARTED | {"insert"=>"products", "documents"=>[{"_id"=>BSON::ObjectId('5682809356c02c15c800000c'), "title"=>"BMW X5", "specs"=>[{"_id"=>BSON::ObjectId('5682809356c02c15c800000d'), "name"=>"make", "value"=>"BMW"}, {"_id"=>BSON::ObjectId('5682809356c02c15c800000e... #D, [2015-12-29T12:46:11.475939 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.insert | SUCCEEDED | 0.000782928s #Works As expected, all updates in save methods product = Product.all.first #D, [2015-12-29T12:46:14.962596 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.find | STARTED | {"find"=>"products", "filter"=>{}, "limit"=>-1} #D, [2015-12-29T12:46:14.964863 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.find | SUCCEEDED | 0.00184706s product.specs = [{name: "make", value: "BMW"}, {name: "model", value: "X5"}] #D, [2015-12-29T12:46:14.985404 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.update | STARTED | {"update"=>"products", "updates"=>[{"q"=>{"_id"=>BSON::ObjectId('5682809356c02c15c800000c')}, "u"=>{"$set"=>{"specs"=>[{"_id"=>BSON::ObjectId('5682809656c02c15c8000010'), "name"=>"make", "value"=>"BMW"}, {"_id"=>BSON::ObjectId('5682809656c02c15c800001... #D, [2015-12-29T12:46:14.986820 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.update | SUCCEEDED | 0.0010425179999999999s #Unexpected autosave product.attributes = {title: "X5 2", specs: [{name: "make", value: "BMW"}, {name: "model", value: "X5"}]} photos = product.photos product.photos = product.photos #D, [2015-12-29T12:46:20.393142 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.update | STARTED | {"update"=>"products", "updates"=>[{"q"=>{"_id"=>BSON::ObjectId('5682809356c02c15c800000c')}, "u"=>{"$set"=>{"photos"=>[{"_id"=>BSON::ObjectId('5682809356c02c15c800000f'), "image"=>"http://example.com/"}]}}, "multi"=>false, "upsert"=>false}], "writeCo... #D, [2015-12-29T12:46:20.394359 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.update | SUCCEEDED | 0.0007998819999999999s #Unexpected autosave that cause next statement bug product.save #D, [2015-12-29T12:46:24.597950 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.update | STARTED | {"update"=>"products", "updates"=>[{"q"=>{"_id"=>BSON::ObjectId('5682809356c02c15c800000c')}, "u"=>{"$set"=>{"title"=>"X5 2"}, "$pushAll"=>{"specs"=>[{"_id"=>BSON::ObjectId('568280a056c02c15c8000012'), "name"=>"make", "value"=>"BMW"}, {"_id"=>BSON::Ob... #D, [2015-12-29T12:46:24.599487 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.update | SUCCEEDED | 0.000984854s #Unexpected pushAll query without clearing old items cause product.reload.specs.size #D, [2015-12-29T12:46:29.502394 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.find | STARTED | {"find"=>"products", "filter"=>{:_id=>BSON::ObjectId('5682809356c02c15c800000c')}} #D, [2015-12-29T12:46:29.503874 #5576] DEBUG -- : MONGODB | 192.168.33.11:27017 | mongo_test.find | SUCCEEDED | 0.001167992s # Unexpected 4