Rails update_attributes 没有保存? [英] Rails update_attributes without save?

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

问题描述

是否有不保存记录的 update_attributes 替代方法?

Is there an alternative to update_attributes that does not save the record?

所以我可以这样做:

@car = Car.new(:make => 'GMC')
#other processing
@car.update_attributes(:model => 'Sierra', :year => "2012", :looks => "Super Sexy, wanna make love to it")
#other processing
@car.save

顺便说一句,我知道我可以@car.model = 'Sierra',但我想在一行中更新它们.

BTW, I know I can @car.model = 'Sierra', but I want to update them all on one line.

推荐答案

我相信您正在寻找的是 assign_attributes.

I believe what you are looking for is assign_attributes.

与update_attributes基本相同,但不保存记录:

It's basically the same as update_attributes but it doesn't save the record:

class User < ActiveRecord::Base
  attr_accessible :name
  attr_accessible :name, :is_admin, :as => :admin
end

user = User.new
user.assign_attributes({ :name => 'Josh', :is_admin => true }) # Raises an ActiveModel::MassAssignmentSecurity::Error
user.assign_attributes({ :name => 'Bob'})
user.name        # => "Bob"
user.is_admin?   # => false
user.new_record? # => true

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

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