"Best practice" for accessing session from model

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 ?

On Sun, Dec 20, 2009 at 5:33 PM, Lin Wj [email protected] 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:

Best of luck!

Cheers,
Robby


Robby R.
Chief Evangelist, Partner

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

+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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Thanks

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

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]
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.