# How do you use Observers?

**URL:** <https://www.ruby-forum.com/t/how-do-you-use-observers/117763>\
**Category:** Rails\
**Created:** [October 19, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763 "2007-10-19T23:48:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 19, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763/1 "2007-10-19T23:48:03Z")

</div>

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:

class UserObserver

def before\_validation(user)  
user.errors.add\_to\_base(“Can’t delete.”) if user.admin?  
end

end

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…

class UserObserver

def before\_validation(user)  
user.admin?  
end

end

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:

class UserObserver

def before\_validation(user)  
raise StandardError if user.admin?  
end

end

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…

class User

validate :there\_can\_be\_only\_one\_admin  
validate :another\_validation  
validate :yet\_another

end

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?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 19, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763/2 "2007-10-19T23:51:27Z")

</div>

Daniel W. wrote:

> class UserObserver
> 
> def before\_validation(user)  
> user.admin?  
> end
> 
> end

Sorry, that should read:

class UserObserver

def before\_validation(user)  
return false if user.admin?  
true  
end

end

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 20, 2007, 7:03am UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763/3 "2007-10-20T07:03:47Z")

</div>

I’m relatively new to rail, and have only done minimal work with  
observers, but I’ve seen them used for caching purposes. To remove  
cached versions of pages after a model has been updated/saved, and  
things of that nature. I’ve never considered using them for model  
validation, as you point out that work belongs in the validation area  
of the model, not in an observer.

On Oct 19, 4:51 pm, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 20, 2007, 7:23am UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763/4 "2007-10-20T07:23:17Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’m relatively new to rail, and have only done minimal work with  
> observers, but I’ve seen them used for caching purposes. To remove  
> cached versions of pages after a model has been updated/saved, and  
> things of that nature.

Interesting. I’ve never dealt with page caching, but that sounds like a  
good candidate for observers.

> I’ve never considered using them for model validation, as  
> you point out that work belongs in the validation area of  
> the model, not in an observer.

And I guess I’m realizing that the observer isn’t the place for  
validation. Looking back it seems silly. The observer’s role is to react  
based on what it observes, not interfere with the models actions.

Thanks for the reply!

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 20, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763/5 "2007-10-20T13:51:53Z")

</div>

Daniel W. wrote:

> end  
> def before\_validation(user)  
> user.admin?  
> end
> 
> end
> 
> 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:

I ran into exactly the same problem when I first began experimenting  
with observers:

[http://dev.rubyonrails.org/ticket/7968](http://dev.rubyonrails.org/ticket/7968)

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 21, 2007, 6:06am UTC](https://www.ruby-forum.com/t/how-do-you-use-observers/117763/6 "2007-10-21T06:06:18Z")

</div>

Wincent C. wrote:

> I ran into exactly the same problem when I first began experimenting  
> with observers:
> 
> [http://dev.rubyonrails.org/ticket/7968](http://dev.rubyonrails.org/ticket/7968)

Interesting activity. I wonder what will come of it. It bothered me more  
when I expected the observer to be able to halt the process of a save.  
Now that I no longer have that expectation it doesn’t bother me so much.

I dunno what the exact role of an observer should be, so I can’t really  
comment on it. Unless, of course, I defined my own role… But that’s up  
to the core team. Though I’m sure a cunning Rubyist could achieve any  
desired effect via a plugin.

Perhaps, perhaps…
