在查看公司项目源码过程中,看到有这样一行代码: waiting_image.update_attributes!(launch_state: Constants::LAUNCH_STATE_ING) 不明白这里的感叹号是起什么作用呢?我知道感叹号有取反的作用,但这里好像不太像是取反,是更新字段 launch_state 的值不为 Constants::LAUNCH_STATE_ING 的意思么?
另外想问问大家如果碰到类似问题,如果去百度搜索,一般用什么关键词会比较容易找到答案?我刚尝试了半天也没从百度上找到什么信息...
save 和 save! 的区别在于,save! 保存失败会抛出异常,save 保存失败结果是 false。你的问题不太清楚
def save(*)
create_or_update
rescue ActiveRecord::RecordInvalid
false
end
# File activerecord/lib/active_record/persistence.rb, line 141
def save!(*)
create_or_update || raise(RecordNotSaved.new("Failed to save the record", self))
end
# File activerecord/lib/active_record/persistence.rb, line 260
def update!(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save!
end
end
update! 调用了 save! 方法,如果保存失败 save! 方法会抛出异常,如果有说明错误的地方请指正。
Updates its receiver just like update but calls save! instead of save, so an exception is raised if the record is invalid.
下次多查下官方 document
alias update_attributes! update!
update!
:Updates its receiver just like update
but calls save!
instead of save
, so an exception is raised if the record is invalid.