Do you use bang methods in controllers (create! save! update!)?

Do you use bang methods in controllers (create! save! update!) with exceptions or do you check if result was a success?

It depends on the context.

If the saving could fail due to the user input, then you probably want to report the problem back to the user so they can correct it. The normal approach would be check if save returns true or false in the controller and act accordingly. It’s generally not a good idea to rely on exceptions for flow control.

If you’re updating a model and you don’t expect the action to ever fail (other than from a bug in your code), then you should use the bang versions. If the validations fail then this will trigger a 500 error instead of a silent failure (which might lead to data being lost). And if you have an exception tracker such as AirBrake then you’ll get an email to let you know something is wrong.

4 Likes

I would add to @andyw8’s post the fact that the return value of those methods might also be of relevance, if instead of true or false, you require the record to be returned instead.

1 Like