一次保存所有更新记录的集合 [英] Save collection of updated records all at once

查看:92
本文介绍了一次保存所有更新记录的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,build方法可用于在保存之前建立关联记录的集合.然后,在调用save时,将验证并保存所有子记录,并且如果存在验证错误,则父记录将具有反映此情况的错误.第一个问题是,这正确吗?

As I understand it, the build method can be used to build up a collection of associated records before saving. Then, when calling save, all the child records will be validated and saved, and if there are validation errors the parent record will have an error reflecting this. First question is, is this correct?

但是我的主要问题是,假设以上内容是有效的,是否有可能对更新执行相同的操作,而不是进行创建?换句话说,有没有一种方法可以更新与父记录关联的集合中的几条记录,然后保存父记录并使所有更新一次发生(如果子代中存在验证错误,则父代中会出现错误) )?

But my main question is, assuming the above is valid, is it possible to do the same thing with updates, not creates? In other words, is there a way to update several records in a collection associated with a parent record, then save the parent record and have all the updates take place at once (with an error in the parent if there are validation errors in the children)?

总而言之,我想知道一种正确的方法来处理一个父记录和几个关联的子记录需要立即更新并保存的情况,而任何错误都将中止整个保存过程.

So to summarize, I'm wondering the right way to handle a case where a parent record and several associated child records need to be updated and saved all at once, with any errors aborting the whole save process.

推荐答案

首先为Transactions +1到@andrea-我不知道的好东西

Firstly +1 to @andrea for Transactions - cool stuff I didn't know

但是最简单的方法是使用accepts_nested_attributes_for 方法用于模型.

But easiest way here to go is to use accepts_nested_attributes_for method for model.

举个例子.我们有两个模型:Post title:stringComment body:string post:references

Lets make an example. We have got two models: Post title:string and Comment body:string post:references

让我们研究一下模型:

class Post < ActiveRecord::Base
  has_many :comments
  validates :title, :presence => true
  accepts_nested_attributes_for :comments # this is our hero
end

class Comment < ActiveRecord::Base
  belongs_to :post
  validates :body, :presence => true
end

您看到的:这里有一些验证.因此,让我们转到rails console进行一些测试:

You see: we have got some validations here. So let's go to rails console to do some tests:

post = Post.new
post.save
#=> false
post.errors
#=> #<OrderedHash {:title=>["can't be blank"]}>
post.title = "My post title"
# now the interesting: adding association
# one comment is ok and second with __empty__ body
post.comments_attributes = [{:body => "My cooment"}, {:body => nil}]
post.save
#=> false
post.errors
#=> #<OrderedHash {:"comments.body"=>["can't be blank"]}>
# Cool! everything works fine
# let's now cleean our comments and add some new valid
post.comments.destroy_all
post.comments_attributes = [{:body => "first comment"}, {:body => "second comment"}]
post.save
#=> true

太好了!一切正常.

现在让我们通过 update 做同样的事情:

Now lets do the same things with update:

post = Post.last
post.comments.count # We have got already two comments with ID:1 and ID:2
#=> 2
# Lets change first comment's body
post.comments_attributes = [{:id => 1, :body => "Changed body"}] # second comment isn't changed
post.save
#=> true
# Now let's check validation
post.comments_attributes => [{:id => 1, :body => nil}]
post.save
#=> false
post.errors
#=> #<OrderedHash {:"comments.body"=>["can't be blank"]}>

这行得通!

那么您怎么使用它.在您的模型中,以相同的方式,在具有常见表单的视图中,但带有fields_for 标记进行关联.另外,您可以对验证与关联使用非常深层的嵌套,它可以完美地发挥作用.

SO how can you use it. In your models the same way, and in views like common forms but with fields_for tag for association. Also you can use very deep nesting for your association with validations nd it will work perfect.

这篇关于一次保存所有更新记录的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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