Embedded Document is not removed in case validate fail
When edit product and delete a row of 'Component' model embedded in Product model, If validation is fail , the deleted document will appear in edit page again.
# Put /products/:id def update @product=Product.find(params[:id]) if(@product.update_attributes(params[:product])) logger.info @product.attribute.components.inspect redirect_to product_path , notice:'update complete' else logger.info @product.attribute.components.inspect render_by_type_id(@product.product_type._id,false,mode='edit') end end
class Product include Mongoid::Document include ScopedSearch::Model embeds_one :attribute ,class_name:"ProductAttribute" accepts_nested_attributes_for :attribute, :product_items ,:lab_test ,allow_destroy: true end class ProductAttribute include Mongoid::Document embedded_in :product embeds_many :components , inverse_of: nil accepts_nested_attributes_for :components, :allow_destroy => true end
class Component include Mongoid::Document embedded_in :product_attribute end
I am not sure if I understand your problem correctly, but please note that destroying through nested attributes is executed on save:
Note that the model will not be destroyed until the parent is saved.
Therefore you should try if Product.create(params[:product]) works. However, I am not sure if this works as expected and this really is what you want. The general use case for nested attribute destruction is to remove an association of an existing object. It seems strange to me to couple object creation and destruction within one param set. So if this does not answer your please try to explain what you want to achieve.