Handling database and model errors
In the previous section, you learned some differences between Active Record and Sequel. One additional difference is their default approach to error handling. By default in Active Record, saving a model instance can return false
if the model is not valid:
model_instance.save # => true or false
This is different than Sequel, where saving a model instance by default raises an exception:
model_instance.save # => model # or raise Sequel::ValidationFailed
Both Active Record and Sequel have options for either behavior, but this shows the philosophical difference between the two libraries. With Active Record, you need to be diligent to make sure you check every call to save
to determine whether it succeeded or failed. If you forget to check a call to save
, your code will continue running, and you may not realize your model instance was never saved. As you learned in Chapter 5, Handling Errors, this is a fail-open design. With Sequel, because...