加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rails activerecord保存方法

发布时间:2020-12-16 20:22:23 所属栏目:百科 来源:网络整理
导读:参见英文答案 how does activerecord know to perform an insert or update?3 家伙,可以用save方法来更新一个记录吗? person = Person.new person.save # rails will insert the new record into the database. 但是,如果我先找到记录,修改它并保存.是否与
参见英文答案 > how does activerecord know to perform an insert or update?3
家伙,可以用save方法来更新一个记录吗?
person = Person.new 
person.save # rails will insert the new record into the database.

但是,如果我先找到记录,修改它并保存.是否与执行更新相同?

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

提前致谢!

解决方法

是. Rails中的ActiveRecord对象在ID参数中保留其身份.如果设置了ID,则Rails将知道使用该ID更新数据库中的记录.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读