开发者

Finding last active time for logged in user

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 probl开发者_如何学编程em 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜