为什么 ActiveRecord::Base#update_attributes 被弃用?
我在 Rails 文档此处中注意到 update_attributes
方法是已弃用:
最新稳定版本的 Rails 已弃用此方法。此处显示了最新的现有版本 (v2.3.8)。
我很想知道为什么它被弃用以及替代它的首选是什么。
现在这对我来说并不是什么大问题,因为该方法在我的 Rails 2.x 应用程序中运行良好。然而,由于预期有一天会升级到 Rails 3,我想开始了解我需要更改哪些内容以及如何以不同的方式完成工作。
I noticed in the Rails docs here that the update_attributes
method is deprecated:
This method is deprecated on the latest stable version of Rails. The last existing version (v2.3.8) is shown here.
I'm curious to know why it was deprecated and what is preferred in its place.
It isn't a matter of enormous consequence for me right now as the method works fine in my Rails 2.x apps. However, in anticipation of upgrading to Rails 3 some day I'd like to start getting a grasp on what I'll need to change and how things are done differently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Rails 6 中它真的被弃用了。这里讨论了原因:
解释是update_attributes 和 update_attributes!只是更新和更新的别名!多年来,它们听起来可能令人困惑(因为它们的版本单一:update_attribute)。
In Rails 6 it became really deprecated. The reasons was discussed here:
The explanation is that both update_attributes and update_attributes! are just alias for update and update! for years and they can sound confusing (because of their singular versions: update_attribute).
Apidock 有点误导,它实际上说的是“已弃用或已移动”;后者是正确的,前者则不是。
ActiveRecord::Base
包括ActiveRecord::Persistence
,它定义了#update_attributes。您可以在 apidock 上查看相关文档,网址为 http://apidock.com/rails/ActiveRecord/Persistence/update_attributes。虽然现在 ActiveResource 中也有此功能,但没有 update_attributes 的共享实现,并且您不应该在那里查找与 ActiveRecord::Base 相关的文档,反之亦然。
Apidock is a little misleading, it actually says "deprecated or moved"; the latter is true, the former is not.
ActiveRecord::Base
includesActiveRecord::Persistence
, which defines #update_attributes. You can view the docs for this on apidock at http://apidock.com/rails/ActiveRecord/Persistence/update_attributes.While this is also in ActiveResource now, there's no shared implementation of update_attributes, and you shouldn't look there for ActiveRecord::Base-related documentation, or vice versa.