ruby-on-rails – 如果失败,`update_attribute`会返回什么?
发布时间:2020-12-17 03:19:10 所属栏目:百科 来源:网络整理
导读:我有以下一段代码 @user = User.find(params[:id])if (@user.activation_status == "active") #some code here @user.update_attribute('activation_status','inactive') # Line 44 #send mail to user that his account is Acivatedelseend 44号线有可能失
我有以下一段代码
@user = User.find(params[:id]) if (@user.activation_status == "active") #some code here @user.update_attribute('activation_status','inactive') # Line 44 #send mail to user that his account is Acivated else end 44号线有可能失败吗?因为数据库内存已满或网络出现故障.在那种情况下会发生什么?如果这会产生问题,那么避免它的更好方法是什么?如果失败,update_attribute会返回什么? 解决方法
以下是update_attribute的源代码:
def update_attribute(name,value) send(name.to_s + '=',value) save(false) end 您可以看到它更新了请求的属性,然后调用save,并将perform_validations设置为false.因此,如果任何保存回调(例如before_save)通过返回false来阻止保存记录,则update_attribute将返回false. 如果存在较低级别的错误,例如数据库中的内存不足,那么我希望数据库驱动程序将此作为异常提升,这将被传递给您的代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |