LoginSignup
0
0

More than 3 years have passed since last update.

saveとsave!の違い

Posted at

備忘録です!!

saveとsave!,destroyとdestroy!の違いを説明していきます。

saveとsave!の違いは「処理が失敗した時の挙動」です。

saveの場合・・・falseを返す

saveの場合・・・例外が発生する。

簡単に説明すると

『処理が失敗する可能性が想定されて、失敗した場合に何かハンドリングしたい場合』はsaveを使う

『処理が必ず成功すると想定されていて、万が一失敗したらそこで処理を中断したい場合』はsave!を使う

例えば、userを登録するときは、バリデーションに引っかかることがあり、失敗する可能性があるので、saveを使います。

destroyでユーザーを削除するときは、必ず成功する、失敗することが考えられないので、destroy!を用います。

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