current_user in Michael Hartl’s Ruby on Rails Tutorial

Hi,

I am reading the RoR Tutorial by Micheal Hartl and trying to make my very first web application at all. Following the book, I arrived now at chapter 8. In this chapter, one defines a couple of methods in the sessions helper:
log_in(user), current_user, and logged_in
With these defined, we put an if logged_in? condition in the site header, to decide which links are displayed in the header (e.g. "login" or "logout" depending on the logged_in status).

Now with the new header, I noticed the following behaviour in the application. Whatever link I click now (even home or help page), the application tries to retrieve a user from the database (even if no user is logged in, the application still hits the database asking for user_id: NULL).

> User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT $1 [["LIMIT", 1]]

I guess that this means that current_user doesn't persist from one request to the other, which means it has to be reloaded for each new request. So my question is:
a) is that really the case?
b) isn't that negatively affecting the performance of the application if it is on a production website?
c) If the answer to b) is yes, is there a way to avoid this behaviour?

Thank you,
pion

4 thoughts on “current_user in Michael Hartl’s Ruby on Rails Tutorial”

  1. hargly recommend gem ‘device’ .

    I don’t know about your tutoruia but usually user_id is stored in session.
    Method cuurent_user is use to be like
    @current_user ||= User.find_by_id(session[:id]) ? session[:id] :false

    loggin_in? like
    true ? session[:id] : false

    Basic idea – after auth we store user_id in session.

    Reply
  2. Your active record call is looking for a users_id in the users table… more than likely you have made a typo somewhere due to the fact rails convention would look for user.id in the users table… notice singular and plural

    Reply

Leave a Comment