问题描述
有没有办法计算出有多少用户使用 devise 主动登录到 Rails 应用?
Is there a way to figure out how many users are actively logged into a rails app using devise?
Rails 3.1
gem '设计', '~> 1.4'gem 'dm-devise', '~> 1.5'
gem 'devise', '~> 1.4'gem 'dm-devise', '~> 1.5'
with : Database Authenticatable
with : Database Authenticatable
我正在专门寻找 ruby 代码.
I am looking for ruby code specifically.
推荐答案
由于与登录相关的所有信息都在会话中处理,因此没有简单的开箱即用的方法.我建议在 ApplicationController 中做一些事情来管理这些信息.
Since all of the information related to being logged in is handled in the session, there's no simple way out of the box. I would recommend doing something in ApplicationController to manage this information.
我们在许多应用程序中使用 before_filter 来跟踪有关每个页面请求的非常具体的信息.您可以执行类似的操作来跟踪他们点击的页面、点击时间以及他们是谁.
We use a before_filter in a number of our applications to track very specific information about each page request. You could do something similar to track what page they hit, when they hit it, and who they were.
然后确定登录用户就像确定他们上次加载页面需要多长时间才能算作登录一样简单,然后根据此在您的视图表中进行选择.类似的东西
Then determining logged in users would be as simple as determining how long ago their last page load would have to be for them to count as logged in, then select on your table of views based on that. Something like
MyPageView.select(["DISTINCT user WHERE created_at > ?", my_threshold_time]) 会给你不同的用户.
MyPageView.select(["DISTINCT user WHERE created_at > ?", my_threshold_time]) would give you the distinct users.
这篇关于有没有办法返回当前登录的设备用户数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!