Created
December 17, 2011 17:17
-
-
Save neovintage/1490792 to your computer and use it in GitHub Desktop.
Mongodb Versioning
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
{ | |
"title": "Creating great content", | |
"content": "Ive got nothing", | |
"comments": [ | |
] | |
"version": 2 | |
"versions": [ | |
{ "title": "Creating great content", "version": 1 } | |
] | |
} |
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
{ | |
"title": "Creating great content", | |
"content": "Ive got nothing", | |
"comments": [ | |
{ "user": "chuck", "content": "Profound words" } | |
] | |
"version": 2 | |
"versions": [ | |
{ "title": "Creating great content", "version": 1 } | |
] | |
} |
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
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 |
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
post = Post.create(:title => "Creating great content") | |
# Now I want to publish it | |
post.content = "Ive got nothing" | |
post.save |
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
post = Post.find(:first, :conditions => {:title => "Creating great content"}) | |
post.comments << Comment.create(:user => "chuck", :content => "Profound words") |
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
post = Post.where(:first, :conditions => {:title => "Creating great content"}) | |
post.comments.build(:user => "lucy", :content => "blah") | |
post.save |
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
# 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