我有一个嵌入文档的项目模型。目前,以下comments_controller 代码将成功为项目添加评论。但是,如果将评论文档推送到 item 的评论数组中失败,我将不知道这一点。
#this does work, but i do not know if the push fails
def create
comment = Comment.new(:text => params[:text])
@item.comments << comment
render :text => comment
end
我想要这样的东西,但@item.comments << 评论不返回真或假:
#this does not work
def create
comment = Comment.new(:text => params[:text])
if @item.comments << comment
render :text => comment
else
render :text => 'oh no'
end
end
文档推送失败时也不会抛出异常:
#this does not work
def create
begin
comment = Comment.new(:text => params[:text])
@item.comments << comment
render :text => comment
rescue Exception => e
render :text => 'oh no'
end
end
谢谢!