-
Notifications
You must be signed in to change notification settings - Fork 57
Description
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.
@current_user = user |
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
.
.
.