我有2个课程......文章和评论(嵌入文章中)。
class Article
include Mongoid::Document
field :name, type: String
field :content, type: String
field :published_on, type: Date
validates_presence_of :name
embeds_many :comments
end
和另一个
class Comment
include Mongoid::Document
field :name, type: String
field :content, type: String
embedded_in :article, :inverse_of => :comments
end
这个mongodb文档的Json表示是:
{
_id:ObjectId("50ae35274b6b5eaa77000001"),
author_id:ObjectId("50ae3b1e4b6b5e8162000001"),
comments:[
{
_id:ObjectId("50ae380e4b6b5e0c34000001"),
name:"fak",
content:"i like this article
}
],
content:"article about nothing",
name:"my sweet article",
}
如何在轨道上使用mongoid在本文档中插入另一条评论?
由于 FAK
答案 0 :(得分:7)
我设法做到了......
在评论控制器:
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create!(params[:comment])
redirect_to @article, :notice => "Comentario criado!"
end
然后我找到了想要添加评论的对象......并创建它
@photox=Photo.find_by(name: "foto xpto")
@photox.comments.create(:comment=>"NOVO COMENTARIO")
谢谢你们......