# Auditing: session\[:user\] reference in after\_save callback

**URL:** <https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661>\
**Category:** Rails\
**Created:** [April 23, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661 "2008-04-23T23:01:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 23, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661/1 "2008-04-23T23:01:16Z")

</div>

I know, it is against the MVC to speak directly to a session object in  
the model but I am looking for a solution to this problem with auditing

I have a SystemLog model which works as a sort of auditor to track  
changes. It has a user\_id that references the user that initiated the  
change and a text string with a description and some other columns.

Whenever I save a model, i want to create a SystemLog object with values  
I can get from the model that is being saved itself, except for the  
user\_id.

How am I supposed to get the session[:user] data neatly to the model the  
way Ruby wants it to? Or is there any other way to realise this auditing  
process which I am overlooking?

The alternative violates the DRY philosophy. After every call to  
@object.save in the controller I manually create a new SystemLog object  
with the @object’s attributes and session[:user] variable.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 23, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661/2 "2008-04-23T23:17:22Z")

</div>

I’m probably mis-understanding the question, but is the problem you  
want information about the user logged in? Could you not do a Find on  
the user table using the ID of the user saved in the [:session] ?

On Apr 23, 5:01 pm, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 24, 2008, 12:00am UTC](https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661/3 "2008-04-24T00:00:13Z")

</div>

Here’s a solution by Pratik, although I have the feeling that he’s, err,  
somehow opposed to it. Just a gut feeling 🙂

[http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model](http://m.onkey.org/2007/10/17/how-to-access-session-cookies-params-request-in-model)

Another solution mentioned in the first Rails Recipes book was to  
(mis)use ActionController::Caching::Sweeping; because of their task,  
Sweepers are observers that have access to the current controller. I  
have yet to use them, but this approach seems to be quite clean to me.  
Take a look at a comment by August L. in Pratiks article, he has  
some example code for this.

HTH.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 24, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661/4 "2008-04-24T16:24:56Z")

</div>

Use an attr\_accessor in your model.

attr\_accessor :user\_id  
after\_save :audit

def audit  
@systemlog = SystemLog.new  
@systemlog.user\_id = self.user\_id  
end

On Apr 23, 6:01 pm, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 24, 2008, 12:01am UTC](https://www.ruby-forum.com/t/auditing-session-user-reference-in-after-save-callback/135661/5 "2008-04-24T00:01:46Z")

</div>

sw0rdfish wrote:

> I’m probably mis-understanding the question, but is the problem you  
> want information about the user logged in? Could you not do a Find on  
> the user table using the ID of the user saved in the [:session] ?
> 
> On Apr 23, 5:01ï¿½pm, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thats the plan yeah. But the thing is I want need the logged in user  
(from the session in my MODEL, since this is the only place I can call  
my before\_ and after\_save functions.

So the hard part is getting the ID from the session to the Model, since  
this is not really Ruby’s way of doing it.
