Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created June 17, 2010 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnunemaker/442227 to your computer and use it in GitHub Desktop.
Save jnunemaker/442227 to your computer and use it in GitHub Desktop.
Atomically remove embedded doc from doc in mongomapper
require 'pp'
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'testing'
class Comment
include MongoMapper::EmbeddedDocument
end
class Post
include MongoMapper::Document
many :comments do
def destroy(id)
proxy_owner.pull(:comments => {:_id => id})
end
end
end
Post.delete_all
comment = Comment.new(:name => 'Peter')
post = Post.create(:title => "Foo", :comments => [comment, Comment.new(:name => 'John')])
pp Post.collection.find.to_a
post.comments.destroy(comment.id)
pp Post.collection.find.to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment