-
Notifications
You must be signed in to change notification settings - Fork 298
Mongoid
saberma edited this page Mar 17, 2011
·
2 revisions
The following code demonstrate how to add and update and destroy multiple embedded document.
require 'mongoid' Mongoid.configure.master = Mongo::Connection.new.db('images') class Image include Mongoid::Document embeds_many :words #lib/mongoid/nested_attributes.rb accepts_nested_attributes_for :words, :allow_destroy => true end class Word include Mongoid::Document embedded_in :image field :text end image = Image.create word100 = image.words.create :text => '100' word200 = image.words.create :text => '200' #normally, the params were post from browser params = { :words => { #lib/mongoid/relations/builders/nested_attributes/many.rb 0 => {:id => word100.id, :text => '1000'}, #update #lib/mongoid/relations/nested_builder.rb 1 => {:id => word200.id, :_destroy => true}, #destroy 2 => {:text => '300'} #create } } #create and update action were taken, but destroy does not work #image.update_attributes params image.words_attributes = params[:words] p image.reload.words