Skip to content

Instantly share code, notes, and snippets.

@neovintage
Created December 17, 2011 17:17
Show Gist options
  • Save neovintage/1490792 to your computer and use it in GitHub Desktop.
Save neovintage/1490792 to your computer and use it in GitHub Desktop.
Mongodb Versioning
{
"title": "Creating great content",
"content": "Ive got nothing",
"comments": [
]
"version": 2
"versions": [
{ "title": "Creating great content", "version": 1 }
]
}
{
"title": "Creating great content",
"content": "Ive got nothing",
"comments": [
{ "user": "chuck", "content": "Profound words" }
]
"version": 2
"versions": [
{ "title": "Creating great content", "version": 1 }
]
}
class Post
include Mongoid::Document
include Mongoid::Versioning
field :title
field :content
embeds_many :comments
end
class Comment
include Mongoid::Document
field :user
field :content
embedded_in :post, :inverse_of => :comments
end
post = Post.create(:title => "Creating great content")
# Now I want to publish it
post.content = "Ive got nothing"
post.save
post = Post.find(:first, :conditions => {:title => "Creating great content"})
post.comments << Comment.create(:user => "chuck", :content => "Profound words")
post = Post.where(:first, :conditions => {:title => "Creating great content"})
post.comments.build(:user => "lucy", :content => "blah")
post.save
# Skip Versioning
Post.skip_callback(:before, :save, :revise)
# Turn it back on
Post.set_callback(:before, :save, :revise)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment