Is the Rails update_attributes method the best choice for doing an update of a model in the database?

感情迁移 提交于 2019-11-28 21:22:24

Update takes an object id parameter and a set of attributes that otherwise work like update_attributes.

So this (from AWDWR 3rd edition)

Order.update(12, :name => "Barney", :email => "barney@bedrock.com")

is equivalent to

Order.find(12).update_attributes(:name => "Barney", :email => "barney@bedrock.com")

So if all you want to do is update a row of known id with a set of attributes then I'd say there's no reason not to use update - it looks like that's the reason they wrote it!

(Is there any way you can get your tutorial to upgrade from 1.1.6? It's pretty old and wasn't a particularly earth-shattering release when it was current. 1.2.6 was better - the last of the 1.xs, if I remember correctly).

Using update_attributes method just suppose that you already have an object, and you would just pass the set of attributes. And the work is done !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!