7

I was wondering if anyone 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 cache fragment specific to 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 ?

Andrew Cetinic
  • 2,805
  • 29
  • 44

1 Answers1

13

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
  • 261
  • 2
  • 5
  • Doesn't UserSession.find return a UserSession object and not a User object? the current user would be UserSession.find.user – John Bachir Aug 15 '10 at 23:30
  • 1
    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 Sep 28 '10 at 03:08