Created
June 17, 2010 14:51
-
-
Save jnunemaker/442227 to your computer and use it in GitHub Desktop.
Atomically remove embedded doc from doc in mongomapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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