如何在Rails中跟踪在线用户? [英] How to track online users in Rails?

查看:87
本文介绍了如何在Rails中跟踪在线用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说一直是个问题,因为Rails没有像Java框架那样的会话跟踪机制,您使用什么方法来跟踪谁当前登录到您的网站?我使用一种简单的方法,即每次用户单击网站上的某个位置时,将last_visited字段设置为当前时间,然后检查(例如)最近10分钟内已更新该字段的用户。除了该方法还有什么其他方法?

This was always a problem for me, as rails doesn't have some mechanisms for session tracking like java frameworks do, what methods do you use to track who is currently logged on your site? I use a simple method by setting up last_visited field with current time every time user clicks somewhere on the site, and then checking for users which have updated that field in (lets say) last 10 minutes. What other methods are there besides this one?

推荐答案

如果您使用的是 active_record_store 用于会话,您可以尝试类似以下操作:

If you are using the active_record_store for your sessions, you can try something like this:

sessions = CGI::Session::ActiveRecordStore::Session.find(:all, :conditions => ["updated_at >= ?", 15.minutes.ago], :order => "created_at ASC")
sessions.each do |session|
  if session.data[:user]
    online_users << User.find(session.data[:user])
  end
end

这篇关于如何在Rails中跟踪在线用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆