Skip to content

current_user instance variable is not being used anywhereΒ #19

@walliby

Description

@walliby

As someone new to rails and ruby, I found the use of the current_user instance variable confusing. On a single settings page hit, I found the current_user method getting invoked 6 times, and 6 hits to the user table to get the current_user. I think the instance variable helped alleviate this in section 8.2.2 of the book where the code looked like this:

@current_user ||= User.find_by(id: session[:user_id])

With the code above, I can understand the value of the instance variable. However further along in the book, this code is refactored to the below and I can no longer see the value of the instance variable.

Would it make sense to change the current_user method to make use of the instance variable again. I've tried thinking of any security vulnerabilities this would introduce but all tests are still passing. Also db calls to user went from 6 down to 2.

  def current_user
    if (!@current_user.nil?)
      return @current_user
    end
  .
  .
  .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions