Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 + Devise: user_signed_in? for a different user in the database?

I'm building an app where I want to add an online status for a given user.

I know that Devise has a method user_signed_in? built in to check if the user who is using the app is signed in or not. But when I try to use it for a different user like this:

user_signed_in?(user)

user.user_signed_in?

I obviously get an undefined method error.

Does Devise have a method for this or do I have to write my own? One approach was to store the online status of a given user in the user model. What's the best solution to this?

like image 769
Max Avatar asked Nov 25 '10 00:11

Max


1 Answers

I have used Devise on my applications and experienced some of the same problems as you when I first began working with it. You are merely using the helper methods incorrectly. If you'd like to check if the current user has a session and is signed in, you use the helper as such:

if user_signed_in?

which is essentially the same statement as:

if !current_user.nil? && current_user.signed_in

If you'd like to check if a user object is signed in, then you call this: (where user is a User Model Object)

if user.signed_in?
like image 55
Ben Avatar answered Oct 26 '22 19:10

Ben