views:

933

answers:

2

Hi Guys I run a sinatra application with mongomapper. I have models called Movie(Document) and Cover(EmbeddedDocument).I embed covers into movies using

@movie.covers << @cover
@movie.save

This works great. when hit @movies.covers I got the array of embedded documents. But I am not able to destroy the embedded document. I tried something like this

@movie.covers.each do |c|
c.destroy
end

NoMethodError: undefined method `destroy' for #<Cover:0xb7b20734>
 from (irb):5
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `call'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `method_missing'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `each'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `send'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `method_missing'
 from (irb):4
 from :0

Can anyone temme how to destroy it? It would be great if someone enlightens me how to update the embedded document.

+4  A: 

Covers is an array, therefore you can do e.g.: @movie.covers.clear @movie.save

or if you want to be selective

@movie.covers.delete_if {|cover| cover.name == "bla"} @movie.save

Piotr Zolnierek
Thanks pzol. It helped me. It would be very helpful if you temme how i can update covers.
Deepak Prasanna
You load the movie. The covers are an array, so you need to choose one, change its fields and then save:For example@movie = Movie.find(:first)@movie.covers[0].name = "updated name"@movie.save
Piotr Zolnierek
+1  A: 

This works if your deleting based on an attribute like a name, but if you already know the id and want to delete it, use this trick from p-rob's tubmlr post:

def destroy
@phone_number = @person.phone_numbers.find(params[:id])
@person.phone_numbers.delete_if{|phone_number| phone_number.id == @phone_number.id}
if @person.save
redirect_to @person
  else
    flash[:error] = "dag, yo."
  end
end

I really struggled with this :(

Nick
What if you don't want to load the whole array into memory before deleting? Isn't there an operation that will remove it on the database side?
obvio171