是否有不保存记录的 update_attributes
替代方案?
所以我可以这样做:
@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'
,但我想在一行中更新它们。
最佳答案
我相信你要找的是assign_attributes
.
和update_attributes基本一样,只是不保存记录:
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
关于ruby-on-rails - Rails update_attributes 没有保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770350/