class Hotel
include Mongoid::Document
field :title, type: String
embeds_many :comments
end
class Comment
include Mongoid::Document
field :text, type: String
belongs_to :hotel
validates :text, presence: true
end
h = Hotel.create('hotel')
=> <
c = Comment.new(text: 'text')
=> <
h.comments << c
=> [#<Comment _id: 52d68f3d7361731d8b040000, text: "text", hotel_id: nil>]
h.save
=> true
Hotel.last.comments
=> []
option 2
h.comments << Comment.new(text: 'new', hotel_id: h.id)
=> [<
h.save
=> true
Hotel.last.comments
=> []
source
share