# Callbacks vs Observer vs "Just adding to controller action"

**URL:** <https://www.ruby-forum.com/t/callbacks-vs-observer-vs-just-adding-to-controller-action/121750>\
**Category:** Rails\
**Created:** [November 30, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/callbacks-vs-observer-vs-just-adding-to-controller-action/121750 "2007-11-30T15:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [November 30, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/callbacks-vs-observer-vs-just-adding-to-controller-action/121750/1 "2007-11-30T15:07:05Z")

</div>

I am setting up auditing for my app where views, updates, and deletes  
are recorded in my Audits table to track the life cycle of an employee  
record. I’ve read how great callbacks and observers are, but there  
isn’t a way to ‘callback’ for simply viewing a page. The simplest  
solution just seems to be manually creating the audit in my show  
action, like this:

def show  
@employee = Employee.find(params[:id])  
Audit.create(  
:user =\> Goldberg.user.name,  
:time =\> Time.now,  
:action =\> ‘Viewed’,  
:employee\_id =\> @employee.id  
)  
end

This works, I’m just wondering if there is a better approach.

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [November 30, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/callbacks-vs-observer-vs-just-adding-to-controller-action/121750/2 "2007-11-30T17:10:11Z")

</div>

On Nov 30, 8:06 am, ebrad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> :user => Goldberg.user.name,
> :time => Time.now,
> 
> ```

Rename the time column to ‘created\_at’ and RAILS will automatically  
set it.

> ```
> :action => 'Viewed',
> :employee_id => @employee.id
> )
> 
> ```
> 
> end
> 
> This works, I’m just wondering if there is a better approach.

Use before\_filter:

class EmployeeController \< ApplicationController  
before\_filter :audit, :only =\> [:show, :update, :delete]

def audit  
@employee = Employee.find(params[:id]) # hope you have some  
authorization check somewhere  
Audit.create(:user =\> …,  
:action =\> params[:action],  
:employee\_id =\> params[:id])  
end

…

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [November 30, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/callbacks-vs-observer-vs-just-adding-to-controller-action/121750/3 "2007-11-30T21:11:44Z")

</div>

“Rename the time column to ‘created\_at’ and RAILS will automatically  
set it.”

- Geesh I should know that by now.

“# hope you have some authorization check somewhere”

- if you mean user authentication, then yes.

very nice! thanks Kevin!
