nested_form icon indicating copy to clipboard operation
nested_form copied to clipboard

Mongoid _destroy is not deleted embedded document using nested_form gem

Open jerryshen opened this issue 12 years ago • 11 comments

I am on Rails 3.1, Mongoid 2.3.3, and using the nested_form gem. In my form, I have the nested_form link_to_add and link_to_remove set up to add and remove an embedded document in my model. The link_to_add helper method works great, but the link_to_remove help method changes are not persisted in MongoDB. In the rails output, I can see the JSON parameter passed to Mongoid has the _destroy: 1 value set but the change is not saved to MongoDB.

Here is the model

class Product
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name,            :type => String

  embeds_many :photos, :as => :photogenic, :cascade_callbacks => true

  accepts_nested_attributes_for :photos,
                            :reject_if => proc { |attributes| attributes['data'].blank? },
                            :allow_destroy => true


end

Here is the params:

"photos_attributes"=>{"0"=>{"_destroy"=>"1", "data_cache"=>"", "id"=>"4ec48e8e6985605b5d000073"}, "1"=>{"_destroy"=>"1", "data_cache"=>"", "id"=>"4ec48e8e6985605b5d000074"}, "2"=>{"_destroy"=>"false", "data_cache"=>"", "id"=>"4ec4925a6985605b5d000083"}, "3"=>{"data_cache"=>"", "_destroy"=>"false"}

I'm sure I missed something, but I haven't been able to figure out why the embedded document isn't removed.

jerryshen avatar Nov 17 '11 05:11 jerryshen

I use nested_form with Mongoid 2.4.5 and Rails 3.2 and works like a charm.

shingara avatar Feb 29 '12 16:02 shingara

I have the same issue as well. The pullAll query's criteria contains other fields in the condition. I think that is causing the pullall to fail

Parameters: {"utf8"=>"✓", "authenticity_token"=>"iShSOBXpgNBEkD+xZ/oN7SazILyV7EVHWxhogMWZHzE=", "activity"=>{"name"=>"test", "description"=>"test", "status"=>"active", "activity_photos_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"4f4ff1d9c1119408d6000012"}, "1"=>{"_destroy"=>"1", "id"=>"4f4ff1d9c1119408d6000013"}, "2"=>{"_destroy"=>"1", "id"=>"4f4ff1d9c1119408d6000014"}, "3"=>{"_destroy"=>"1", "id"=>"4f4ff1d9c1119408d6000015"}}}, "commit"=>"Update Activity", "vendor_id"=>"4f4c732cc11194ad4e00001a", "id"=>"4f4ff1d9c1119408d6000011"}

MONGODB ninjatravelco_development['activities'].update({"_id"=>BSON::ObjectId('4f4ff1d9c1119408d6000011')}, {"$pullAll"=>{"activity_photos"=>[{"_id"=>BSON::ObjectId('4f4ff1d9c1119408d6000012'), "_type"=>"ActivityPhoto", "asset_file_name"=>nil, "asset_content_type"=>nil, "asset_file_size"=>nil, "asset_updated_at"=>nil, "processing"=>false}, {"_id"=>BSON::ObjectId('4f4ff1d9c1119408d6000013'), "_type"=>"ActivityPhoto", "asset_file_name"=>nil, "asset_content_type"=>nil, "asset_file_size"=>nil, "asset_updated_at"=>nil, "processing"=>false}, {"_id"=>BSON::ObjectId('4f4ff1d9c1119408d6000014'), "_type"=>"ActivityPhoto", "asset_file_name"=>nil, "asset_content_type"=>nil, "asset_file_size"=>nil, "asset_updated_at"=>nil, "processing"=>false}, {"_id"=>BSON::ObjectId('4f4ff1d9c1119408d6000015'), "_type"=>"ActivityPhoto", "asset_file_name"=>nil, "asset_content_type"=>nil, "asset_file_size"=>nil, "asset_updated_at"=>nil, "processing"=>false}]}})

mcheung610 avatar Mar 01 '12 22:03 mcheung610

8 months later, still no solution on this issue for rails 3.1?

jerryshen avatar Jul 03 '12 09:07 jerryshen

Could you please push a test application reproducing this issue? Thanks.

lest avatar Sep 18 '12 06:09 lest

I'm afraid even 3 years after this is still a problem... If you're still reading, would you mind posting your solution in case you've found any ? (Using Rails 4.1.1)

Startouf avatar Jan 11 '15 22:01 Startouf

Rails 4.1.8 works when using (not without allow_destroy: true):

accepts_nested_attributes_for :association, allow_destroy: true

eksoverzero avatar Jan 12 '15 18:01 eksoverzero

Thanks. I did a big update to Rails 4.2 and it fixed it.

Startouf avatar Jan 12 '15 21:01 Startouf

I still encounter problems with either embedded_many or embedded_one and accepts_nested_attributes_for with Mongoid 4.0.0 and Rails either 4.1 or 4.2.

I have found different behavior of update (doesn't work with _destroy and reject_if) and (association)_attributes= methods (works correctly!).

In short, accepts_nested_attributes_for sets options only for (association)_attributes= methods by closuring passed object, but this object isn't saved in any another place. https://github.com/mongoid/mongoid/blob/v4.0.0/lib/mongoid/attributes/nested.rb#L54

But update using Mongoid::Attributes::Processing at some moment passes empty hash as options (so, then attribute builders doesn't take into account real allow_destroy and reject_if): https://github.com/mongoid/mongoid/blob/v4.0.0/lib/mongoid/attributes/processing.rb#L139

Conclusion: It's mongoid issue.

ales-vilchytski avatar Jan 16 '15 19:01 ales-vilchytski

i use mongoid (4.0.2) and rails 4.2, it can not work too!

TangMonk avatar Mar 28 '15 08:03 TangMonk

@ales-vilchytski Correct! I had the same issues with _destroy in accepts_nested_attributes_for Please fix it.

thiensubs avatar Nov 03 '16 15:11 thiensubs

same issue also with embedded_many relation

malachheb avatar Mar 16 '20 16:03 malachheb