Clarification: I’m asking how YOU use them, not how TO use them.
I ask because after reading a post over at Pivotal Blabs (a very good
blog, btw), it appears the Observer isn’t quite meant to do what I
thought it was. Example:
user.errors.add_to_base(“Can’t delete.”) if user.admin?
I would expect the above to halt the saving of the object because it now
fails validation. But it doesn’t. The record gets saved anyway.
Then I tried…
Perhaps returning false would halt the process. Not so. In fact, the
only way to stop a save inside an observer is to raise an exception:
raise StandardError if user.admin?
But I don’t want to raise an exception. It’s not an exceptional
situation – the model simply failed validation.
Meanwhile, I’ve recently discovered you can do this…
I’ve seen the validate method before, but I didn’t know you could stack
it like that. Anywho, if you add errors to the object inside any of
these methods, validation fails, which is fine.
But I’m left with wondering… what’s the purpose of an Observer outside
of sending e-mails and logging messages?
So, how do you use Observers in your projects?