# Using session-type data in an ActiveRecord callback

**URL:** <https://www.ruby-forum.com/t/using-session-type-data-in-an-activerecord-callback/103445>\
**Category:** Rails\
**Created:** [May 6, 2007, 2:47am UTC](https://www.ruby-forum.com/t/using-session-type-data-in-an-activerecord-callback/103445 "2007-05-06T02:47:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jseidel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b19c9b/32.png) [@jseidel](https://www.ruby-forum.com/u/jseidel)\
**Post date:** [May 6, 2007, 2:47am UTC](https://www.ruby-forum.com/t/using-session-type-data-in-an-activerecord-callback/103445/1 "2007-05-06T02:47:49Z")

</div>

Any suggestions on how best to gain access to session-type information  
while in an ActiveRecord callback, such as after\_save?

I am encrypting some information in the database, using the  
generalized encryption/decryption handler in “Agile Web D.  
With Rails” (p 375ff). I got it working, and now I want to reference  
a session field (a user PIN) which is NOT stored in the database…  
for security reasons.

I can’t seem to gain access to the session method:

1. attempting to use session[‘user’][‘pin’] gives me an “undefined  
variable or method”
2. attempting to use UserController.session[‘user’][‘pin’] gives me a  
message saying that it’s expecting an integer, not a string.

Any ideas as to how to either (1) gain access to the session hash or  
(2) provide my own application-wide storage mechanism?

Thanks…jon

---

<div class="post-metadata">

**Author:** ![jseidel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b19c9b/32.png) [@jseidel](https://www.ruby-forum.com/u/jseidel)\
**Post date:** [May 6, 2007, 4:23am UTC](https://www.ruby-forum.com/t/using-session-type-data-in-an-activerecord-callback/103445/2 "2007-05-06T04:23:53Z")

</div>

I wouldn’t do it this way since it kind of muddy’s up the controller &  
model portions of the MVC.

Instead you should create an function that will set the PIN in the  
user model like this.  
attr\_accessor :pin

Then in your controller just pass it to a user object

user = User.find\_by\_id(session[:user][:id])  
user.pin = session[:user][:pin]  
user.save!

in the Model

after\_save  
# do whatever with the pin  
user.pin # this will call it now that you have set it  
end

That’s the basic gist of it, I haven’t tested any of this could but I  
hope its clear enough to understand.

hope that helps,  
Jim

---

<div class="post-metadata">

**Author:** ![jseidel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b19c9b/32.png) [@jseidel](https://www.ruby-forum.com/u/jseidel)\
**Post date:** [May 6, 2007, 4:25am UTC](https://www.ruby-forum.com/t/using-session-type-data-in-an-activerecord-callback/103445/3 "2007-05-06T04:25:53Z")

</div>

oops I meant instead of this

> after\_save  
> # do whatever with the pin  
> user.pin # this will call it now that you have set it  
> end

do this

after\_save  
self.pin #to call the current user instances pin.  
end

---

<div class="post-metadata">

**Author:** ![jseidel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b19c9b/32.png) [@jseidel](https://www.ruby-forum.com/u/jseidel)\
**Post date:** [May 6, 2007, 6:54am UTC](https://www.ruby-forum.com/t/using-session-type-data-in-an-activerecord-callback/103445/4 "2007-05-06T06:54:01Z")

</div>

Very cool, Jim… thank you very much… just the kind of guidance I  
was looking for. I’ll give it a shot tomorrow.

…jon
