# "Best practice" for accessing session from model

**URL:** <https://www.ruby-forum.com/t/best-practice-for-accessing-session-from-model/180519>\
**Category:** Rails\
**Created:** [December 21, 2009, 2:33am UTC](https://www.ruby-forum.com/t/best-practice-for-accessing-session-from-model/180519 "2009-12-21T02:33:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [December 21, 2009, 2:33am UTC](https://www.ruby-forum.com/t/best-practice-for-accessing-session-from-model/180519/1 "2009-12-21T02:33:40Z")

</div>

i need to do update pm a “updated\_by” field with a session variable  
“user” on a model after\_save.

but accessing session variables from models is supposedly bad

whats the usual best practice for this kinda scenario ?

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [December 21, 2009, 3:03am UTC](https://www.ruby-forum.com/t/best-practice-for-accessing-session-from-model/180519/2 "2009-12-21T03:03:53Z")

</div>

On Sun, Dec 20, 2009 at 5:33 PM, Lin Wj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i need to do update pm a “updated\_by” field with a session variable  
> “user” on a model after\_save.
> 
> but accessing session variables from models is supposedly bad

ActiveRecord shouldn’t know anything about the controller/view  
environment… it only knows how to deal with information that has  
been passed to it.

> whats the usual best practice for this kinda scenario ?

So the best option is to pass the data to ActiveRecord. There are a  
number of ways that you could do this. You could do this through your  
users model. You might look at Userstamp for inspiration:

- [GitHub - delynn/userstamp: This Rails plugin extends ActiveRecord::Base to add automatic updating of created\_by and updated\_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created\_(at/on) and updated\_(at/on) attributes.](http://github.com/delynn/userstamp)

Best of luck!

Cheers,  
Robby

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
Web Design and Development with Ruby on Rails

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

+1 408 372 7466  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [December 21, 2009, 6:52am UTC](https://www.ruby-forum.com/t/best-practice-for-accessing-session-from-model/180519/3 "2009-12-21T06:52:40Z")

</div>

Thanks

totally got lost ploughing thru Userstamp’s code  
probably a good time for me to start learning some metaprogramming

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [December 21, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/best-practice-for-accessing-session-from-model/180519/4 "2009-12-21T15:26:14Z")

</div>

Lin Wj wrote:

> Thanks
> 
> totally got lost ploughing thru Userstamp’s code  
> probably a good time for me to start learning some metaprogramming

Not that I spend much time digging though the code of Userstamper  
myself, but I believe this this the key to what makes the magic happen:

* * *

The Userstamp module that we included into our ApplicationController  
uses the setter method to  
set which user is currently making the request. By default the  
‘set\_stampers’ method works perfectly  
with the  
RestfulAuthentication[[http://svn.techno-weenie.net/projects/plugins/restful\_authentication](http://svn.techno-weenie.net/projects/plugins/restful_authentication)]  
plug-in:

## def set\_stampers User.stamper = self.current\_user end

The current\_user get passed into into the model layer from the  
controller layer. That is, as opposed to the model layer pulling the  
current\_user directly from the session, which would break the MVC  
pattern. There are many ways to pass that information down into the  
model but, the point is that it’s the controller layer’s responsibility  
to push state information down into the model layer.
