# Observers and outside methods

**URL:** <https://www.ruby-forum.com/t/observers-and-outside-methods/162810>\
**Category:** Rails\
**Created:** [March 14, 2009, 12:21am UTC](https://www.ruby-forum.com/t/observers-and-outside-methods/162810 "2009-03-14T00:21:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [March 14, 2009, 12:21am UTC](https://www.ruby-forum.com/t/observers-and-outside-methods/162810/1 "2009-03-14T00:21:57Z")

</div>

I’m thinking about using observers for my situation. Basically  
whenever an object is created, I want a message to be sent to a user  
via a PM system I made. At the moment this is happening within the  
create method of the controller. I tried putting this in a callback in  
the model, but it seems methods such as the URLs (users\_path) and  
current\_user don’t work in models. Since observers are also a type of  
model, I was wondering if these methods don’t work in them either?

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [March 16, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/observers-and-outside-methods/162810/2 "2009-03-16T16:07:41Z")

</div>

you can include them in the model if you want. Mybe i get some  
screaming for this as models and views should be completely separate  
and independent (so if you change the routes, for example, you would  
have to change this method, for you its not a problem because you  
worte it, but in a project with more people, it would net be a normal  
thing to do.

just include ActionView::Helpers::UrlHelper. and you can get methods  
like url\_for.

I would defenitely put such method in a callback in the model:

after\_create :announce

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [March 16, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/observers-and-outside-methods/162810/3 "2009-03-16T16:17:39Z")

</div>

If what I want to accomplish affects only the model with which I’m  
working,  
I put the behavior in a callback on the model. If it affects other  
related  
models, I put it in an observer. Otherwise, I put it in the controller  
or  
some other non-ActiveRecord library code. In those cases where I’ve  
needed a  
URL, either I build and use it in the controller, or I build it in the  
controller and pass it to the thing that will do the work, e.g., a  
mailer or  
other notifier.

One reason I wouldn’t put notification code in an observer is that it  
will  
fire no matter how you create the model: via the normal flow through the  
Rails stack, in script/console, etc. I usually only want the  
notification if  
the work is being done through the web app, not script/console.

Food for thought.

Regards,  
Craig

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [March 16, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/observers-and-outside-methods/162810/4 "2009-03-16T21:31:35Z")

</div>

Oh, ok. Thanks for the replies!
