The Authlogic record method. What does this do

2020-07-10 08:34发布

问题:

I came across this method called record that Ryan bates uses in his authlogic Railscast and can't seem to understand what it does. I have been through the documentation but I can't seem to follow how that helper is useful.

def current_user
  return @current_user if defined?(@current_user)

  current_user_session && current_user_session.record
end

What I want to know is does this simply fetch the record from the database and why is it different from the standard way of fetching data from the database.

Thank you.

回答1:

This "record" method is a belongs_to relation between the UserSession and the User models. So based on the Session, it returns the User.

However based on the documentation, it's not "record" anymore. But "user". So your current_user helper method should be the following :

def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
end