控制器中批量更新的最佳实践
我想知道在控制器中允许批量编辑/更新的最佳实践是什么。我真的找不到有关该主题的文章或指南。
I am wondering what is the best practice for allowing bulk edit / update in the controller. I really can't find articles or guides on the subject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我看到您用 REST 标记了您的问题。
要以 REST 方式执行此操作,您需要将集合或更新本身视为资源。
假设您正在使用 Product 对象。
您可能会 PUT 到 /product_batches/[someidentifier],这将调用 ProductBatchesController#update,但随后您会想知道 [someidentifier] 中会包含什么。您可以将 ProductBatch 设为 单一资源,然后就不需要 id 了。
更好的方法可能是 POST 到 /product_bulk_updates,这将调用 ProductBulkUpdatesController#create
这是另一个线程:
通过 REST (RESTful) API 进行批量收集操作
I see that you tagged your question with REST.
To do it RESTfully, you need to think about the collection or the update itself as the resource.
Say you are working with Product objects.
You might PUT to /product_batches/[some identifier], which would call ProductBatchesController#update, but then you are stuck wondering what goes in [some identifier]. You could make ProductBatch a singular resource and then you wouldn't need an id.
Better might be to POST to /product_bulk_updates, which would call ProductBulkUpdatesController#create
Here's another thread:
Bulk Collection Manipulation through a REST (RESTful) API
我认为没有标准的方法。您可以使用
update_attributes
(例如使用 PostsController):或者使用
update_all
,但请注意,回调和验证都不会被调用:I don't think there is a standard way. You may use
update_attributes
(example with PostsController):Or use
update_all
, but notice that neither callbacks nor validations will be called:如果您有幸使用 Rails 3,那么您应该确保查看 Rails 2 中的 ActiveRecord::Relation#update_all 或 ActiveRecord::Base#update_all:
构建单个 SQL 更新语句比执行完整的 SQL 往返来更新元素要好得多。
重要提示:这确实是使用 SQL 更新语句的批量更新。它不会实例化任何 ActiveRecord 对象,因为更新纯粹是在 SQL 中执行的。因此,ActiveRecord 回调和验证将不会被调用。
上面 URL 中的示例:
注意:据我从互联网上的帖子来看,这个功能在 Rails 3.0.3、3.0.7 - 3.0.9 中处于有缺陷和损坏之间。
我直到 3.1.0 才发现这个功能,所以我无法证实。
If you're fortunate enough to be working in Rails 3 then you should make sure to check out ActiveRecord::Relation#update_all or ActiveRecord::Base#update_all in Rails 2:
It's much bettter to construct a single SQL update statement than do a full SQL round trip to update the elements.
Important Note: This is truely a bulk update using a SQL update statement. It will not instantiate any ActiveRecord objects since the update is performed purely in SQL. As such ActiveRecord callbacks and validations will not be called.
Examples from the URL above:
Note: As far as I can tell from posts on the internet, this functionality was somewhere between buggy and broken in Rails 3.0.3, 3.0.7 - 3.0.9.
I didn't discover the feature until 3.1.0, so I can't corroborate.
对@Robert Head 的答案进行了一些小的补充。在Rails官方参考中有一个章节“什么是REST?”http://guides.rubyonrails.org/v2.3.11/routing.html#what-is-rest
本章引用了REST 的作者 Roy Fielding 的博士论文。在本文的“5.2.1.1资源和资源标识符”章节中我们可以读到:
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1
因此,在这里定义一个
ProductCollection
资源(及其 7 个自己的 RESTful 操作)将非常适合整个 REST 概念。Some minor additions to the @Robert Head's answer. In the Rails official reference there's a chapter "What is REST?" http://guides.rubyonrails.org/v2.3.11/routing.html#what-is-rest
This chapter references the REST's author Roy Fielding’s doctoral thesis. In Chapter "5.2.1.1 Resources and Resource Identifiers" of this thesis we can read:
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1
So defining here a
ProductCollection
resource - with its 7 own RESTful actions - would nicely fit into the whole REST concept.我还没有尝试过这个,它不是真正的模型级批量操作,但它看起来很有趣。
batch_api gem 允许累积请求并一次性发送它们。 Facebook也有类似的机制。
I have not tried this, and it is not truely model level bulk operation, but nevertheless it seems interesting.
The batch_api gem allows one to accumulate requests and send them in one go. Facebook has similar mechanism too.