-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 7.0.5
-
Component/s: Attributes
-
None
-
Environment:Mongo version: 4.2.1
Mongoid version: 7.0.5
Ruby version: 2.6.5
Rails version: 6.0.1
When updating an object's embedded relation via nested attributes, the nested object is not being updated and the nested attributes are saved in the parent level.
How to reproduce
Suppose we have the following two models.
# app/modes/human.rb class Human include Mongoid::Document embeds_one :cat, class_name: "Cat" accepts_nested_attributes_for :cat end # app/modes/human.rb class Cat include Mongoid::Document field :name end
In the Rails console:
>> human = Human.new cat_attributes: { name: "Phoebe" } => #<Human _id: 5dc5519dc2889857ac7bea13, > >> human.cat => #<Cat _id: 5dc551a5c2889857ac7bea14, name: "Phoebe"> >> human.save => true >> human.reload => #<Human _id: 5dc5519dc2889857ac7bea13, > >> human.cat => #<Cat _id: 5dc551a5c2889857ac7bea14, name: "Phoebe"> >> # So far all good. => nil >> human.attributes = { cat_attributes: { name: "Irida" } } => {:cat_attributes=>{:name=>"Irida"}} >> human.save => true >> human.cat => #<Cat _id: 5dc551a5c2889857ac7bea14, name: "Irida"> >> human.reload => #<Human _id: 5dc5519dc2889857ac7bea13, > >> human.cat => #<Cat _id: 5dc551a5c2889857ac7bea14, name: "Phoebe"> >> human.as_json => {"_id"=>{"$oid"=>"5dc5519dc2889857ac7bea13"}, "cat"=>{"_id"=>{"$oid"=>"5dc551a5c2889857ac7bea14"}, "name"=>"Phoebe"}, "name"=>"Irida"} >> # Human now has a "name" attribute...