Setup:
class Post include Mongoid::Document embeds_many :company_tags embeds_many :user_tags end class CompanyTag include Mongoid::Document field :title, type: String embedded_in :post end class UserTag include Mongoid::Document field :title, type: String embedded_in :post end
Initialization:
post = Post.create post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'a'}], user_tags: [{id: BSON::ObjectId.new, title: 'b'}]) post.save post.reload p post.company_tags # [#<CompanyTag .., title: "a">] p post.user_tags # [#<UserTag .., title: "b">]
7.3.3:
post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'c'}], user_tags: []) post.save p post.company_tags # [#<CompanyTag .., title: "c">] CORRECT p post.user_tags # [] CORRECT post.reload p post.company_tags # [#<CompanyTag .., title: "c">] CORRECT p post.user_tags # [] CORRECT
7.3.4:
post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'c'}], user_tags: []) post.save p post.company_tags # [#<CompanyTag .., title: "c">] CORRECT p post.user_tags # [] CORRECT post.reload p post.company_tags # [#<CompanyTag .., title: "a">, #<CompanyTag .., title: "c">] WRONG p post.user_tags # [] CORRECT
Queries:
# 7.3.3 # "u"=>{"$unset"=>{"user_tags"=>true}, "$set"=>{"company_tags"=>[{"_id"=>BSON::ObjectId('627d54d358af14bf60afa0b9'), "title"=>"c"}]}} # 7.3.4 # "u"=>{"$unset"=>{"user_tags"=>true}, "$push"=>{"company_tags"=>{"$each"=>[{"_id"=>BSON::ObjectId('627d555558af14c2327e7592'), "title"=>"c"}]}}}
- is caused by
-
MONGOID-5197 assign_attributes for embedded field raising conflict after assign of empty array
- Closed