什么时候使用保存!创造!和update_attributes的!在Rails的? [英] When do I use save!, create! and update_attributes! in Rails?

查看:501
本文介绍了什么时候使用保存!创造!和update_attributes的!在Rails的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出何时使用砰!版本保存和更新记录?我已阅读并听说你不需要他们,如果你只是保存一个记录或更新一个属性,如果你确信没有什么应该不会错,或总是使用它们控制器之外。我想我是偏执具有多事情得到保存那么就有失败再有就是在数据库中的数据不完整。目前Rails项目我工作的超过50%完成,目前不包含任何刘海。我有一些自定义的方法我打电话的模式,更新或创建多个记录,并担心,如果他们应该在某种交易。

I'm trying to figure out when to use the bang! versions for saving and updating records? I've read and heard that you don't need them if you're just saving one record or updating a single attribute, if you're confident nothing should go wrong, or to always use them outside of a controller. I guess I'm paranoid about having multiple things getting saved then something fails then there is incomplete data in the DB. The current Rails project I'm working on is over 50% complete and currently doesn't contain any bangs. I have some custom methods I'm calling in models that update or create multiple records and worry if they should be in some sort of transaction.

抱歉,如果这似乎散,但我只是想弄清楚如何使用ActiveRecord的储蓄能力,正确的,让我的生活更轻松,一点没有压力到底。感谢您的时间。

Sorry if this seems scattered but I'm just trying to figure how to use the saving capabilities in ActiveRecord correctly and make my life easier and little more stress free in the end. Thanks for your time.

推荐答案

通常要使用非爆炸版本在控制器。这样的逻辑是这样的:

Generally you want to use the non-bang versions in your controllers. This allows logic like this:

def update
  @model = Model.find params[:id]
  if @model.update_attributes params[:model] #returns true of false
     # handle success
  else
     # handle failure
  end
end

我发现自己使用的爆炸版本有很多的测试,当我想确保我知道,如果事情不验证,并且不会被保存。我肯定浪费时间的调试测试,是失败,因为改变了模型验证,如果我用爆炸的版本这将是显而易见的。

I find myself using the bang versions a lot in tests when I want to make sure I know if something doesn't validate, and isn't saved. I've definitely wasted time debugging tests that were failing because of changed model validations, which would be obvious if I used the bang versions.

例如。

it "should do something" do
   m = Model.create! :foo => 'bar' # will raise an error on validation failure             
   m.should do_something
end

在没有数据库中的无效数据方面,你应该用ActiveRecord的验证处理它(比如 validates_ presence_of:USER_ID ),或定义自己的验证模型方法。 (http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html)这应该prevent从保存,如果你的数据是无效的发生。如果你真的偏执,你可以添加一些约束你的数据库。检查的ActiveRecord ::迁移文档了解如何设置唯一的索引和其他数据库的限制在你的迁移。

In terms of not having invalid data in the database, you should be handling this with ActiveRecord validations (e.g. validates_presence_of :user_id), or defining your own validate method in the model. (http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html) This should prevent saves from occurring if your data isn't valid. If you're really paranoid you can add some constraints to your database. Check the ActiveRecord::Migration docs for how to set up unique indexes and other database constraints in your migrations.

另外,在我的经验,要避免使用任何自定义保存或创建方法,只要有可能。如果你重新实现包含在ActiveRecord的功能,你最终付出代价的道路。 <一href="http://matthewpaulmoore.com/post/5190436725/ruby-on-rails-$c$c-quality-checklist">http://matthewpaulmoore.com/post/5190436725/ruby-on-rails-$c$c-quality-checklist有更多的话要说这一点。

Also in my experience you want to avoid using any custom save or create method whenever possible. If you re-implement functionality included in ActiveRecord you end up paying a price down the road. http://matthewpaulmoore.com/post/5190436725/ruby-on-rails-code-quality-checklist has more to say on this.

这篇关于什么时候使用保存!创造!和update_attributes的!在Rails的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆