Thoughtbot folks have a great article on not expecting exceptions – save bang your head, active record will drive you mad. I’ll admit, just like the poster, I used to use save! in controllers to DRY my code. And have a global rescue_from in application.rb. But over the time, I changed the camp and now I’m fully in that “Don’t expect expectations” camp. Some things are more important that DRYing 3 lines of code.
But I’d want to take this a step further. When you’re not expecting something to fail, always use the methods that raise exceptions on failure.
So I strongly disagree with the poster on this :
I think ActiveRecord::Base#save! and ActiveRecord::Base.update_attributes! should be pulled from the public API
I would advocate just the opposite for certain cases. In many of the code reviews we’ve done via ActionRails, the following pattern was seen in many of the models :
def do_something
self.foo = 'bar'
save
end
def create_items
names.each {|n| self.items.create :name => n }
end
In the snippets above, it’s not checking for cases where the save fails. And for good reasons that they’re not likely to fail as code is changing some very minor. But in these scenarios, a failure would be an unexpected situation. Hence you should always use save! or create!.
There could be easily be any unexpected reasons the above save could fail. And using save! protects you from those situations and help catch those minor programming mistakes early, which otherwise could prove to be very costly in terms of time/efforts. So the above code should really be :
def do_something
self.foo = 'bar'
save!
end
def create_items
names.each {|n| self.items.create! :name => n }
end
However, if you’re using exceptions for flow control, this practise won’t always help you :
def create
@user = User.create! params[:user]
redirect_to @user
rescue ActiveRecord::RecordNotSaved
flash[:notice] = 'Unable to create user'
render :new
end
As this catches the exception ActiveRecord::RecordNotSaved, unexpected save! failures from your model methods/callbacks will get caught too. And hide the real error.
Moral of the story :
1,Don’t expect exceptions
2,Use methods throwing exceptions when you’re not expecting a failure. For example, everywhere you’re not checking if save or create fails when working with Active Record objects, always use save! and create! instead.
分享到:
相关推荐
Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save Easy Save ...
AutoSave_AutoSave_AutoSave_无标题模型.skp
DataSave DataSave DataSave DataSave DataSave DataSave
简要说明 语法:intweb_reg_save_param(constchar*ParamName,<listofAttributes>,LAST); 参数说明:ParamName:存放得到的动态内容的参数名称listofAttributes:其它属性,包括:Notfound,LB,RB,RelFra
Unity EasySave3中文图文教程详解-万能数据保存插件多平台支持 - ChinarCSDN的博客 - CSDN博客_files
EasySave2和EasySave3插件源码; 里面附带中文使用文档.
Unity EasySave3插件 与 中文图文教程详解-万能数据保存插件多平台支持,EasySave2和EasySave3插件源码; 里面附带中文使用文档.
本地存储Easy Save3
Easy Save - The Complete Save Load Asset v3.3.0Easy Save - The Complete Save Load Asset v3.3.0Easy Save - The Complete Save Load Asset v3.3.0
Easy Save - The Complete Save Data Serializer System 3.5.6.unity
原始SaveAction插件在idea更新到2023.1版本后会报错无法使用,所以有人修复了,源码放在https://github.com/fishermans/intellij-plugin-save-actions,在根目录调用脚本gradlew.bat build后构建,会在根目录下build...
很是麻烦 AutoSave是个自动保存剪贴版内容 目前版本只能保存其中的文本信息 的小工具 纯粹是为了提高平时上网查找资料的效率与自娱 并可以根据你的需要快速地在“自动保存”与“手工保存”两种功能之间切换 ">平时...
Unity3D U3D 热门插件EasySave3中文简单入门教程,非EasySave2,这个插件使用起来本来就挺简单,有中文翻译更是让那些英语渣而且懒得机翻的童鞋省心了。
-- 如果没有用flash,需要设置该属性为false[true] (true / false) In case you don't use any flash - JavaScript communication, you shuold set this setting to false - this will save some CPU and will ...
《Protect Our Environment》Save Our World! ppt【完美版课共.pdf
save, load, serialize, playerprefs, storage, upload, download, serialization, serialisation, save file, import, export file, encryption, cloud, excel, obfuscate, playerprefs, auto save
WWW Power Save和Legacy Power Save的一些资料
Unity 插件, Easy Save 2 插件包,用于保存和提取,下载解压,导入即可,亲测好用,请放心下载!
file save 下载