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 8381433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:41:58+00:00 2026-06-09T16:41:58+00:00

I am going through the great Michael Hartl tutorial to build ruby app here

  • 0

I am going through the great Michael Hartl tutorial to build ruby app here.

I am trying to understand the concept of how to create a session and I am stuck in understanding this line:

self.current_user = user

in this method:

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end
end

I understand the whole concept of creating a cookie with the user_token.

But I don’t understand what does self.current_user = user means and why is it even necessary to keep this line of code – I have the cookie with the token – why do I need to know the current user?

Also, where does this “self” is being stored – it is not like a flash[:success] parameter I can see in one of my views. so I don’t understand where it is.

there are also these 2 methods in the same module:

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end 

And still I am trying to connect the dots of the purpose for this mysterious current user – is its purpose is to create @current_user global variable to use in the views?

If so – why there are there these 2 duplicated functions def current_user=(user) and def current_user

  • 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-09T16:41:59+00:00Added an answer on June 9, 2026 at 4:41 pm

    A few things.

    First, you’re reading the method names wrong (which is not surprising given how cryptic ruby method naming can be). def current_user=(user) is actually read as defining the method current_user= that takes an argument user, whereas def current_user defines a method current_user that takes no arguments. These are referred to respectively as setters and getters.

    Here’s a reference: Ruby (programming language): What are setters and getters in Ruby?

    So that explains the duplication. On to your next question.

    I don’t understand what does self.current_user = user means

    self is a topic unto itself, worthy of its own discussion, so I won’t even try to explain it (here’s one reference out of many). For the purposes of this question it’s just important to remember that in order to set instance variables, you need to prefix your assignment with self, even within the class (where for other purposes it would be implicit). The rest of the line is a call to the current_user= setter method I mentioned above, with the argument user.

    why is it even necessary to keep this line of code – I have the cookie with the token – why do I need to know the current user?

    The reason it’s necessary is that you don’t want to be looking up the user from the token every time you need to get the current user. Take a look at the getter method:

    def current_user
      @current_user ||= User.find_by_remember_token(cookies[:remember_token])
    end
    

    What this says is: if I haven’t looked up and set the instance variable @current_user yet, then look it up; if I have already set it, then just return it. That saves a lot of looking up.

    I think that answers your questions. There are a lot of deeper issues (self, etc.) which you can find more information about elsewhere. Here’s one discussion of why you need to include self in setters on SO: Why do Ruby setters need "self." qualification within the class?

    UPDATE: Small clarification, that last link about using self for setters within the class is actually a bit off-topic, since you’re calling it within a module and not directly from a class. In the context of a module, the self in self.current_user = user will become the class that the module is included inside of, e.g. User.current_user if it was called within the class User, etc. Again, another topic of discussion unto itself…

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

Sidebar

Related Questions

I'm going through Michael Hartl's Ruby on Rails 3.2 Tutorial and I'm confused about
Going through the microsoft authentication tutorial listed here they have you create a master
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
I was going through great articles on C++ POD, Trivial and Standard Layout classes
Any help or pointers would be great on this, I'm going through the ASP.NET
After going through the Appendix A, C# Coding Style Conventions of the great book
I am going through Write Yourself a Scheme in Haskell . Its a great
I'm going through the Pro Git book , trying to get my head around
After going through hell trying to install the latest version of postgresql and psycopg2
I'm going through a phase of trying to avoid temporary variables and over-use of

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.