require 'mongo/model'Example of Composite Objects using [Mongo Model][mongodb_model].
Models are just ordinary Ruby Objects, so You can combine and mix it as You wish. The only differences are
id attribute.id, but have _parent - reference to the main object.[Callbacks][callbacks], [validations][validations] and [conversions][conversions] works on embedded objects the same way as on the main.
In this example we’ll create simple Blog Application and see how to embed Comments into the Post.
require 'mongo/model'Connecting to test database and cleaning it before starting.
Mongo::Model.default_database_name = :default_test
Mongo::Model.default_database.clearDefining Post.
class Post
inherit Mongo::Model
collection :posts
attr_accessor :textWe using plain Ruby Array as container for Comments.
def comments; @comments ||= [] end
endDefining Comment.
class Comment
inherit Mongo::Model
attr_accessor :textWe need a way to access Post from Comment, every child object has _parent reference, let’s
use it.
alias_method :post, :_parent
endCreating Post with Comments and saving it to database.
post = Post.new text: 'Zerg infestation found on Tarsonis!'
post.comments << Comment.new(text: "I can't believe it.")
post.saveRetrieving from database.
post = Post.first
p post.comments.class # => Array
p post.comments.size # => 1
p post.comments.first.text # => "I can't believe it"
p post.comments.first.post == post # => trueAdding another comment.
post.comments << Comment.new(text: "Me too, but it's true.")
post.saveReading updated post.
post = Post.first
p post.comments.size # => 2In this example we covered how to create and use composite objects (embedded objects) - use plain Ruby Objects and the Driver will figure out how to save and restore it.