导轨:保存更新记录收集一次全部 [英] Rails: Save collection of updated records all at once

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

问题描述

据我了解,在建立方法可用于保存之前建立相关记录的集合。然后,调用时保存,所有的子记录将被验证并保存,如果有验证错误父记录将反映这一错误。第一个问题是,这是正确的?

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.

推荐答案

首先+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.

让我们做出了榜样。我们有两种型号:职衔:字符串评论正文:字符串后:引用

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

您看到:我们在这里得到了一些验证。因此,让我们去导轨控制台做一些测试:

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

大!一切工作正常。

Great! All works fine.

现在可以用做同样的事情的更新的:

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天全站免登陆