【发布时间】:2011-09-07 03:06:19
【问题描述】:
我有一个创建评论对象的例行操作,其中一个答案 has_many cmets。
我遇到了一个奇怪的错误,当一个新的评论发布到该答案时,它偶尔会删除该答案的现有 cmets。新评论也会被删除。
MongoHQ 好心地指导我解决这个问题:https://github.com/mongoid/mongoid/issues/1173,但我使用的是 mongoid2.2,它应该没有这个问题。
class Answer
include Mongoid::Document
has_many :comments, :dependent => :delete
class Comment
include Mongoid::Document
belongs_to :answer
# answer controller
def comment
answer=Answer.find(params[:id])
c=Comment.new(:text=>params[:text],:user_id=>current_user.id,:answer_id=>answer.id)
success=c.save
respond_to do |format|
format.json {
render :json=>c.to_json
else
render :json=>{"failure"=>c.errors}.to_json
end
}
end
end
此处包含更多详细信息: http://pastie.org/2488895
【问题讨论】: