Raising an ActiveRecord error on RecordNotFound

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 04:21:06

问题


Is there a way of doing a find_by_x that raises an exception if the record is not found?


回答1:


added "!" at the end:

User.find_by_name "a" => return nil

User.find_by_name! "a" => raise error


来源:https://stackoverflow.com/questions/1400467/raising-an-activerecord-error-on-recordnotfound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!