Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if object.valid? の後のsaveの書き方 #77

Open
ohta-rh opened this issue Aug 7, 2013 · 1 comment
Open

if object.valid? の後のsaveの書き方 #77

ohta-rh opened this issue Aug 7, 2013 · 1 comment
Labels

Comments

@ohta-rh
Copy link
Contributor

ohta-rh commented Aug 7, 2013

どんな書き方しますか?

#もうvalidationがとおってるはずなので、validate: falseでsaveするパターン
# ここで落ちたら超例外のはず。(DB死んでるとか)
if objetct.valid?
  object.save validate: false
end

# validと同じタイミングでsaveするパターン
if object.valid? && object.save
end

# 個人的に推奨したい
# saveたたくとvalidが走るので、そもそも二つ書かない
if object.save
end

#直撃でsave!
if object.save!
end

# これもよいかも
# valid?の後にエラーが出たら例外でおちてくれる。
if object.valid?
  object.save!
end

どんなかんじでしょう?

@sakamoto-rh
Copy link

【まとめ】
validation以降に対象のobjectに修正を加えた場合、
save!を付けること。

修正を加えない場合は.saveにすること。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants