Scott Walter wrote:> Hi,
>
> I am trying to use the created_by and updated_by
> mixins to record who created/updated a record.
> There''s a requirement where I need to provide a class
> variable called current_user in my User model.
>
> So I have a User model with this added:
>
> @@current_user = nil
>
> def self.current_user
> @@current_user
> end
>
> def self.current_user= abd
> @@current_user = abd
> end
>
> Within my login code I set the current_user with
> something link this:
>
> User.current_user = User.find("username","password")
>
> However when I go to another controller and try to
> print out the current user via User.current_user it
> always returns nil.
>
> I added a log statement that appears in the User model
> but outside any method that simple prints "here I am".
> What I noticed is that it prints twice, once on my
> login controller and also on my second controller.
>
> So is my user.rb model file getting reloaded on each
> request causing @@current_user to be reset to nil,
> since I have @@current_user=nil?
>
> Thanks for anyone''s help. I have been going around in
> circles on this.
Hi Scott - I hope you have resolved this by now, but just in case...
your Rails app should not expect anything outside of the database,
session or flash to persist from request to request. Class variables
might disappear because of Rails and the application being reloaded
(CGI), or application classes being reloaded (WEBrick in development
mode) or successive requests being handled by different processes, with
their own copies of Rails and the application (FastCGI or SCGI).
regards
Justin