I have a routine action that creates an comment object, where an answer has_many comments.
I am experiencing a weird bug that occasionally deletes the existing comments for an answer when a new comment is posted to that answer. The new comment gets deleted as well.
MongoHQ kindly directed me to this issue: https://github.com/mongoid/mongoid/issues/1173, but i am using mongoid2.2, which is supposed not to have this issue.
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
More details included here:
http://pastie.org/2488895
I fixed it by downgrading to mongoid 2.0.2. This seems to be a very specific bug that only happens under a 1-N relationship, and if you are on mongohq. Hope it gets fixed in mongoid >2.2