views:

30

answers:

1

I am using ruby on rails session to store a user cookie to keep them logged in, so I can't just update a last_seen column after they login. I'm trying to figure out the best way to find out if a user has been active in the last day. This is what I have so far but I'm wondering if there's a better way:

class ApplicationController
  before_filter :update_last_seen

private
  def update_last_seen
    if (DateTime.now - 1.day) < current_user.last_seen
      current_user.last_seen = DateTime.now
      current_user.save
    end
  end
end

The problem with this is that it is going to get called with every request. Is there any way to avoid this? Does the session cookie get updated somehow when a user is active?

+1  A: 

No, that is exactly how you should go about it. before_filters are a great way to keep track of things like that, and are how authlogic implements it's authentication system as well. There's no way to do processing on one request "per day". If you use Authlogic as your authentication method, it has last-login time built in. You can see an example of it here.

Mike Trpcic
Ok thanks, it looks like update_last_request_at! might be the method Authlogic uses: http://github.com/qoobaa/sms/blob/41e2ee0ea1425b44db29150a940f153aaaac50d7/vendor/gems/authlogic-1.4.1/lib/authlogic/session/timeout.rb.
TenJack