Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:56:32+00:00 2026-06-17T08:56:32+00:00

So I’ve got this task where I’m supposed to output a list of the

  • 0

So I’ve got this task where I’m supposed to output a list of the last five logins by the currently logged in user.

So like this:

Person A logs in at 12.00 12/12/12.

Person A logs in again at 12.00 12/01/13.

And so on.

This needs to work up to a list of five, once that limit is reached the first login (12.00 12/12/12 in this case) is to be taken out of view.

What I’ve got so far is a Login system with remember tokens and some simple authorization for the login itself (doing it from scratch and going back to some details fom Michael Hartl’s tutorial). Right now, I can list the current time that the user logged in, but not the previous ‘versions’.

So, this is my view:

<% if signed_in? %>
<h1>Welcome, <%= current_user.name %>!</h1>
<p>You were last logged in on:</p>
<ul>
    <li><%= current_user.lastlogin %></li>
</ul>
<%= link_to "Sign out", signout_path, method: "delete", class: "btn btn-large" %>
<% else %>
... (state for non logged in users)
<% end %>

This is my user class:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :lastlogin
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  ...(validation following this)

And this is my sessions_controller

class SessionsController < ApplicationController
def new
end

def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
        sign_in user
        redirect_to root_path
else
        flash.now[:error] = 'Invalid email/password combination'
    render 'new'
  end
end

And here we have the sign_in function

module SessionsHelper
 def sign_in(user)
 # user.update_attribute(:lastlogin, Time.now)
   user.touch(:lastlogin)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
 end
end

I’m looking for ideas on how to tackle this problem.

Another thing that I’ve forgotten to mention is that the view layer needs to be modified to some extent so as to loop through the five login times and output them in order, as opposed to hard coding in five list items.

One thing that I’m thinking of is setting up a relationship between a table with five fields corresponding to one user. And then upon sign in (or sign out) take the :lastlogin value and push it into a field in the table.

Then when all fields have been filled, look at the first date and replace it with the new.

So what I’m thinking is something like

def sign_in
  user.touch(:lastlogin)
  # push :lastlogin value into field in LoginsTable
  cookies.permanent[:remember_token] = user.remember_token
  self.current_user = user
end

I am quite new to Ruby on Rails and might be way off here. I’ve tried searching for functionality like the one I’m explaining here, and all I’ve gotten is various plugins/full-out gems that handle this and everything else when it comes to Authentication and so forth, which isn’t really ‘needed’ in this case as it’s just a small time project for myself.

Cheers!

Edit:

The futile attempt at pushing into the user_logins table.

module SessionsHelper
  def sign_in(user)
    user.update_attribute(:lastlogin, Time.now)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
    login_time = UserLogins.create(:login_time Time.now, :user_id current_user)
  end
end

Now this is obviously not working. I’m not too sure on exactly how to loop through the login_times either in the view layer. But I reckon I’ll get it working eventually, now that you put me on the right track 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:56:33+00:00Added an answer on June 17, 2026 at 8:56 am

    You’ll get yourself into a messy pickle if you hardcode 5 fields to hold the last 5 login dates, instead consider having a table to store login history for all users. A simple table “user_logins” with id, user_id and login_time would suffice.
    Write to this table every time the user logs in, then in your view just select the top 5 results ordered by login_time, descending.

    Hope that makes sense!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.