0
0

More than 3 years have passed since last update.

update_attributesとupdate_attributes!の違い

Posted at

結論

どちらのメソッドもデータベースに更新する直前にvalidationを実行する。
validationに妥当性がなかった場合に

update_attributes falseを返す

update_attributes! 例外処理を返す

という違いがある。

入力必須であるnameカラムを持つUserモデルを例にすると

update_attributes

user.update_attributes(name: nil)
=> false

update_attributes!

user.update_attributes!(name: nil)
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

それぞれ上記の処理をする。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0