I’m trying to find a descent solution for the following problem:
Lets say I have a model called List. When you create a list, a name is
required (validation is in place).
But when a user creates a list in the application, a simular object
needs to be created on a remote system through the use of an API.
First idea would be to add your validation (presence) on name create a
after_validation callback. So after the validation the object would be
created on the remote system.
The problem however is, when the remote object creation fails (no
connection, udated remote api with an extra restriction, …) I want to
notify the user of this and don’t let the local object be created.
My first thought was adding some error handling in the callback method
and when that fails, throwing the error into the object errors hash.
The thing I overlooked was that “save” clears out the errors hash and
reruns the valid? method, that in turn runs the validations again but
doesn’t look at the result of callbacks.
So how do you solve such an issue in a clean way?