public
Last active

Atomically remove embedded doc from doc in mongomapper

  • Download Gist
atomic_embedded_pull.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.