Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Mongoid _destroy is not deleted embedded document using nested_form gem #117

jerryshen opened this Issue · 9 comments

9 participants


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


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.

@ryanb ryanb was assigned

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


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}]}})


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


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


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)


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

accepts_nested_attributes_for :association, allow_destroy: true

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


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.

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):

Conclusion: It's mongoid issue.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.