Is the code for transaction good and optimal in rails 3.1.3? Is the code for transaction good and optimal in rails 3.1.3? sqlite sqlite

Is the code for transaction good and optimal in rails 3.1.3?


First off: redirect_to or render don't act like a return statement. They just assign some headers in a response that is currently being prepared.

About transactions:

if @out_log.save && @part.save

The above code is sure to lead to inconsistent states in your DB: what if first save is successful and the second one isn't? It's even hard to imagine.

The solution is quite simple: use save! (with the exclamation at the end). This way if your validation fails your whole transaction will be rolled back (an exception will be raised by save! instead of returning false as the save does).