I am currently working through http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec:current_user]1 and I am having trouble understanding what the difference between the two current_user methods. In my other method sign_in there is a called to self.current_user but I am having trouble understanding which current_user is called and what the other current_user method is actually doing.
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
def signed_in?
!current_user.nil?
end
private
def user_from_remember_token
# using the * makes us pass two values [id,salt]
User.authenticate_with_salt(*remember_token)
end
def remember_token
cookies.signed[:remember_token] || [nil,nil]
end
end
The
current_user=method is called when you assign to the current_user attribute of your object, and thecurrent_usermethod is called when you don’t.So
self.current_user = usercallscurrent_user=while!current_user.nil?callscurrent_user.