我什么时候使用保存!,创建!和更新属性!在 Rails 中? [英] When do I use save!, create! and update_attributes! in Rails?

查看:13
本文介绍了我什么时候使用保存!,创建!和更新属性!在 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.

推荐答案

通常您希望在控制器中使用非 bang 版本.这允许这样的逻辑:

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

我发现自己在测试中经常使用 bang 版本,当我想确保我知道某些内容是否未验证且未保存时.我肯定浪费了时间调试由于更改模型验证而失败的测试,如果我使用 bang 版本,这将是显而易见的.

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) 如果您的数据无效,这应该可以防止保存.如果您真的很偏执,可以向数据库添加一些约束.查看 ActiveRecord::Migration 文档,了解如何在迁移中设置唯一索引和其他数据库约束.

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 中包含的功能,您最终会付出代价.http://matthewpaulmoore.com/post/5190436725/ruby-on-rails-code-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.

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

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