47
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsのsaveメソッドでtrue/falseでなくオブジェクトを返したい場合

Posted at

通常の場合ActiveRecordのsaveメソッドの戻り値はtrue/falseですが、そうでなく更新結果のオブジェクトを返したい場合以下のように記述するとできます

user = User.find(1)
user.tap(&:save)

これやるとsaveした結果が戻り値として返ってきます。githubのRailsの開発状況を眺めていたら、この話題があったので。。。
以上です。

47
38
4

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
47
38

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?