Rails accepts nested attributes and mongo error on delete

37 Views Asked by At

I'm using models on Mongo and Rails' accepts_nested_attributes_for. For specific scenerio, when I try to add attachment_ids in first item and also I remove second item I receive mongo error:

[40]: Updating the path 'section9.significant_events.0.attachment_ids' would create a conflict at 'section9.significant_events' (on localhost:27017, legacy retry, attempt 1)

Here are the params passed to controller:

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"xxxxx",
 "section9"=>
  {
   "significant_events_attributes"=>
    {"0"=>{"_destroy"=>"false", "description"=>"", "info_location"=>"", "attachment_ids"=>["", "62f4bd4fba962ee7ae1e96fd"], "id"=>"62f4b604ba962ee7ae1e96bd"},
     "1"=>{"_destroy"=>"1", "description"=>"", "info_location"=>"", "attachment_ids"=>[""], "id"=>"62f4b772ba962ee7ae1e96ca"}}
  },
 "commit"=>"Save",
 "section_map"=>"section9"}

And the relations are:

Section9
  embeds_many :significant_events
  accepts_nested_attributes_for :significant_events, allow_destroy: true


SignificantEvent
  has_and_belongs_to_many :attachments, class_name: 'Edoc'=
  embedded_in :section9
1

There are 1 best solutions below

1
Rakesh On

I think field should appear either in $set, or in $setOnInsert. But it should not be in both.