# Observers or callbacks?

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

<div class="post-metadata">

**Author:** ![tekwiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tekwiz](https://www.ruby-forum.com/u/tekwiz)\
**Post date:** [March 21, 2009, 12:12am UTC](https://www.ruby-forum.com/t/observers-or-callbacks/163499/1 "2009-03-21T00:12:49Z")

</div>

Why use an Observer for a single model rather than using a callback in  
the ActiveRecord object? They seem like they work identically. Is  
there a difference I’m missing?

For example, these seem like they would do the same thing:

class User \< ActiveRecord::Base  
after\_create :send\_welcome

```
protected
  def send_welcome
    UserMailer.send_welcome(self)
  end

```

end

class UserObserver \< ActiveRecord::Observer  
def after\_create(user)  
UserMailer.send\_welcome(user)  
end  
end

– TW

---

<div class="post-metadata">

**Author:** ![tekwiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tekwiz](https://www.ruby-forum.com/u/tekwiz)\
**Post date:** [March 21, 2009, 2:16am UTC](https://www.ruby-forum.com/t/observers-or-callbacks/163499/2 "2009-03-21T02:16:43Z")

</div>

Hi tekwiz,

This is excerpt is from Agile Web D. with Rails:

Callbacks are a ï¬ne technique, but they can sometimes result in a  
model class  
taking on responsibilities that arenâ€™t really related to the nature of  
the model.  
For example, on page 385 we created a callback that generated a log  
message  
when an order was created. That functionality isnâ€™t really part of the  
basic  
Order classâ€”we put it there because thatâ€™s where the callback  
executed.  
Active Record observers overcome that limitation. An observer  
transparently  
links itself into a model class, registering itself for callbacks as  
if it were part  
of the model but without requiring any changes in the model itself.

---

<div class="post-metadata">

**Author:** ![tekwiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tekwiz](https://www.ruby-forum.com/u/tekwiz)\
**Post date:** [March 21, 2009, 2:53am UTC](https://www.ruby-forum.com/t/observers-or-callbacks/163499/3 "2009-03-21T02:53:33Z")

</div>

Thanks elioncho! I understand that it is a best practice to separate  
certain functionality out of models into observers.

I guess I should clarify my question… Is there any _technical_  
reason to use observers instead of callbacks or visa versa.

– TW

---

<div class="post-metadata">

**Author:** ![tekwiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tekwiz](https://www.ruby-forum.com/u/tekwiz)\
**Post date:** [May 8, 2009, 3:51am UTC](https://www.ruby-forum.com/t/observers-or-callbacks/163499/4 "2009-05-08T03:51:19Z")

</div>

I’d really like to know the answer to that too!
