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 ? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Dec 20, 2009 at 5:33 PM, Lin Wj <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 badActiveRecord 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: * http://github.com/delynn/userstamp Best of luck! Cheers, Robby -- Robby Russell Chief Evangelist, Partner PLANET ARGON, LLC Web Design and Development with Ruby on Rails http://planetargon.com/ +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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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 -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Lin Wj wrote:> Thanks > > totally got lost ploughing thru Userstamp''s code > probably a good time for me to start learning some metaprogrammingNot 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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.