Here's the easiest way:
- Whenever the
current_user
does any action, hisupdated_at
will be set asTime.now
.
application_controller.rb
after_action :update_user_online, if: :user_signed_in?
private
def update_user_online
current_user.try :touch
end
- And we will just say that the
user
isonline
if he wasupdated_at
during the last2.minutes
user.rb
def online?
updated_at > 2.minutes.ago
end
- Now we can get
true
orfalse
if we make a call like@user.online?
users/show.html.erb
<%= @user.online? %>
That's it!ðŸ¤
Liked this article? Please follow me! It will really motivate me to post more fun stuff!
Top comments (0)