Is there a way to return the number of devise users currently logged in?

前端 未结 2 811
清歌不尽
清歌不尽 2021-01-22 09:55

Is there a way to figure out how many users are actively logged into a rails app using devise?

Rails 3.1

gem \'devise\', \'~> 1.4\' gem \'dm

2条回答
  •  走了就别回头了
    2021-01-22 10:38

    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.

    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]) would give you the distinct users.

提交回复
热议问题