轨道ActiveRecord的保存方法 [英] rails activerecord save method

查看:100
本文介绍了轨道ActiveRecord的保存方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人,可以保存方法可以用来更新记录?

guys, can the save method be used to update a record?

person = Person.new 
person.save # rails will insert the new record into the database.

不过,如果我先找到一个记录,修改并保存。是一样的进行更新?

however, if i find a record first, modify it and save it. is it the same as performing a update?

person = Person.find(:first, :condition => "id = 1") 
person.name = "my_new_name" 
person.save # is this save performing a update or insert?

在此先感谢!

Thanks in advance!

推荐答案

是的。在Rails的一个ActiveRecord对象将保留其在ID参数标识。如果ID设置,Rails会知道要更新的记录与该ID的数据库。

Yes. An ActiveRecord object in Rails retains its identity in the ID parameter. If the ID is set, Rails will know to update the record in the database with that ID.

保存,其实,主要的方式来创建,更新,或以任何方式对象保存到数据库中。其他的方法,如 update_attributes的是使用保存其核心只是糖。

save is, in fact, the primary way to create, update, or in any way save an object to the database. Other methods like update_attributes are just sugar that use save at their core.

这篇关于轨道ActiveRecord的保存方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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