1

I am following along with Michael Hartl's rails tutorial. I am confused with a portion of the sessions helper module.

In the following code can someone please clarify if the current_user in the logged_in? method refers to the actual method current_user and is read as the user is only logged_in? if the method current_user dies not return nil?

OR

Is the current_user referring to the instance variable @current_user inside of the current_user method

Any clarification is greatly appreciated. Thank you so much!

module SessionsHelper
  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Returns the current logged-in user (if any).
  def current_user
    if session[:user_id]
      @current_user ||= User.find_by(id: session[:user_id]) 
    end
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end 
end
2
  • The former is correct. logged_in? means: The return value from the current_user method is not (!) nil?. Commented Dec 14, 2021 at 20:53
  • thank you for the clarification that really helps me understand what is going on. Cheers! Commented Dec 14, 2021 at 21:13

1 Answer 1

1

Every module that you put in the app/helpers directory is included into your controllers. When you include a module you add it to the ancestors chain of the class so it behaves as if the method was defined in the class itself.

So to explain the current_user method line by line it checks if the session has the key :user_id and then assigns the instance variable @current_user to the controller if its not already set. This is known as a memoizing getter method - and avoids firing a database query to fetch the user every time its called.

In logged_id? you're using that getter method:

# Returns true if the user is logged in, false otherwise.
def logged_in?
  !current_user.nil?
end 

It could actually be written as !@current_user.nil?. But there is a big caveat - in Ruby you can reference instance variables that are not set without error. So if you made a typo and wrote!@curent_user.nil? it would just fail unexpectedly as its always nil.

When creating an authentication system there should also only be one method that knows how to fetch the user, and all other code should go through that system so that the implementation details don't leak out all over the place.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the very thorough and detailed explanation. Very very helpful. Much Appreciated. Thank you!
Technically speaking current_user.nil? is session[:user_id] is falsey or @current_user.nil? rather than strictly @current_user.nil? because theoretically @current_user.nil? #=> false but session[:user_id] #=> nil and thus current_user.nil? != @current_user.nil?
@engineersmnky yes - you're correct. I forgot about the return value of the if expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.