views:

936

answers:

1

Hey guys,

I was wondering if anyway knows how I can access the user ID from the session object in Rails using the Authlogic gem ?

I have a sweeper that runs to expire a fragment specific for a user:

 def expire_cache_for(record)
    ActiveRecord::Base.logger.debug "team: #{record.team_id}"
    ActiveRecord::Base.logger.debug("User: #{session}")

    # Expire a fragment
    expire_fragment(:controller => 'groups',  :action => "team_#{record.team_id}_user_#{current_user.id}")
  end

How can I access the user ID from my sweeper to expire the fragment using Authlogic ?

+7  A: 

You should be able to do this by using your UserSession model (or whatever model you've added that derives from Authlogic::Session::Base):

current_user = UserSession.find
id = current_user && current_user.record.id
John Pignata
thanks! This helped me too
GreenRails
Doesn't UserSession.find return a UserSession object and not a User object? the current user would be UserSession.find.user
John
John Pignata is correct. The session stores the acts_as_authentic model in a variable record, which is the same regardless of the name of the acts_as_authentic model.
Brian King