Hi ! I remember reading somewhere there was a way to "inject" the current session into model objects. Does someone remember how to do it ? I can''t seem to find it on the wiki. I searched using Google too, and I still don''t find that. Thanks, François
Fancois, Of course, the obvious thing to do is to pass the session when you invoke a method. I assume from your question that you want the session to be available to the model without explicitly passing it with each method call. Some people have suggested using a class variable. My understanding is that ruby''s threading model may permit this. I think the safest approach is to add a reference to the thread object. To make a user object available to models I created a set_user method that is invoked in the controller with a before_filter, like so: protected def set_user Thread.current[:user_access] = UserAccess.new(@session[:user]) end then you can access it in your model like so: thing_access = Thread.current[:user_access] So far this is working well for me. I hope this is helpful. -Kelly On 9/14/05, François Beausoleil <fbeausoleil-IQIa899fVSs@public.gmane.org> wrote:> > Hi ! > > I remember reading somewhere there was a way to "inject" the current > session into model objects. Does someone remember how to do it ? I > can''t seem to find it on the wiki. > > I searched using Google too, and I still don''t find that. > > Thanks, > François > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Pardon me - that should be François, of course. :-) -Kelly On 9/14/05, Kelly Felkins <railsinator-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Fancois, > > Of course, the obvious thing to do is to pass the session when you invoke > a method. I assume from your question that you want the session to be > available to the model without explicitly passing it with each method call. > > Some people have suggested using a class variable. My understanding is > that ruby''s threading model may permit this. > > I think the safest approach is to add a reference to the thread object. To > make a user object available to models I created a set_user method that is > invoked in the controller with a before_filter, like so: > > protected > def set_user > Thread.current[:user_access] = UserAccess.new(@session[:user]) > end > > then you can access it in your model like so: > > thing_access = Thread.current[:user_access] > > So far this is working well for me. > > I hope this is helpful. > > -Kelly > > > On 9/14/05, François Beausoleil <fbeausoleil-IQIa899fVSs@public.gmane.org> wrote: > > > > Hi ! > > > > I remember reading somewhere there was a way to "inject" the current > > session into model objects. Does someone remember how to do it ? I > > can''t seem to find it on the wiki. > > > > I searched using Google too, and I still don''t find that. > > > > Thanks, > > François > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi ! Kelly Felkins said the following on 2005-09-14 21:59:> I think the safest approach is to add a reference to the thread object. > To make a user object available to models I created a set_user method > that is invoked in the controller with a before_filter, like so: > > protected > def set_user > Thread.current[:user_access] = UserAccess.new(@session[:user]) > end > > then you can access it in your model like so: > > thing_access = Thread.current[:user_access] > > So far this is working well for me.Thanks, I thought about that too. That''s probably what I''ll end up doing. Thanks again ! François