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

前端 未结 2 808
清歌不尽
清歌不尽 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.

    0 讨论(0)
  • 2021-01-22 10:40

    I wrote a blog post about this. We had to do this at work, and this solution seems to fit our needs.

    You should add last_request_at and logged_in to your user model. last_request_at can be grabbed from the user_session information on the request. Logged_in should be set on sign_in / sign_out.

    0 讨论(0)
提交回复
热议问题