Rails: How to Show User's "Last Seen At" Time

Rails: how to show user's last seen at time?

How about this:

  1. Create a migration to add a new field to users to store the date and time the user was last seen:

    rails g migration add_last_seen_at_to_users last_seen_at:datetime
  2. Add a before action callback to your application controller:

    before_action :set_last_seen_at, if: proc { user_signed_in? }

    private
    def set_last_seen_at
    current_user.update_attribute(:last_seen_at, Time.current)
    end

This way, on every request (i.e. activity) that the current user performs, his/her last seen at attribute is updated to the current time.

Please note, however, that this may take up some of your app's resources if you have many users who are logged in, because this will execute before every controller action requested by someone who is logged in.

If performance is a concern, consider adding the following throttle mechanism to step 2 (in this example, throttling at 15 minutes):

before_action :set_last_seen_at, if: proc { user_signed_in? && (session[:last_seen_at] == nil || session[:last_seen_at] < 15.minutes.ago) }

private
def set_last_seen_at
current_user.update_attribute(:last_seen_at, Time.current)
session[:last_seen_at] = Time.current
end

User last online at time using devise?

Add a column in a users table last_seen_at & update it every time using touch.

class ApplicationController
before_action :record_last_seen_at

private

def record_last_seen_at
if current_user
current_user.touch :last_seen_at
end
end
end

How can I obtain user's last active time when using Devise?

The way I would implement this is to add a last_active_at datetime column to the User model, then putting something in my controller:

class ApplicationController
before_filter :record_user_activity

private

def record_user_activity
if current_user
current_user.touch :last_active_at
end
end
end

Finding last active time for logged in user

No, that is exactly how you should go about it. before_filters are a great way to keep track of things like that, and are how authlogic implements it's authentication system as well. There's no way to do processing on one request "per day". If you use Authlogic as your authentication method, it has last-login time built in. You can see an example of it here.

Devise: Get Date of User's last activity

It's not really devise's job to keep track of this - it is only an authentication library. What constitutes an action is business logic, you should implement it into your models.
Perhaps the simplest way to do this would be adding in your models something like:

belongs_to :user, :touch => true

This means that every time a resource has been updated, the user that resource belongs to will have the updated_at field set to the current datetime.

Display last logged in details using Devise in Rails 3

The Devise documentation outlines the trackable module which will do what you want. In your user model, include the :trackable module like so:

  devise :database_authenticatable, 
...
:trackable

And make sure your database has the right fields. Not sure how do this if you already have a user table, but adding fields with the right names and types should do the trick. My migration to create my users table looks like so:

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email

t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable

t.timestamps
end
end

def self.down
drop_table :users
end
end

The t.trackable will add the correct fields. In my user model, they're as follows:

sign_in_count: integer, 
current_sign_in_at: timestamp,
last_sign_in_at: timestamp,
current_sign_in_ip: string,
last_sign_in_ip: string

Then you can just do user.last_sign_in_at and check the strftime documentation on how to output the time in the format you want.

How to display last post for each user in ruby on rails?

you may try this code, change @posts to u.posts

<% @users.each do |u| %>
<% if u.posts.size > 0 %>
<%= u.posts.last.total %>
<%= u.email %>
<% end %>
<% end %>


Related Topics



Leave a reply



Submit