Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

current_user instance variable is not being used anywhere #19

Open
walliby opened this issue Dec 29, 2023 · 0 comments
Open

current_user instance variable is not being used anywhere #19

walliby opened this issue Dec 29, 2023 · 0 comments

Comments

@walliby
Copy link

walliby commented Dec 29, 2023

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
  .
  .
  .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant