I've been looking at a project for a login module, but I'm not sure exactly what this helper method is doing:
def self.authenticate(login, pass)
u = User.first(:login => login)
return 开发者_JS百科nil if u.nil?
return u if User.encrypt(pass, u.salt) == u.hashed_password
nil
end
Why not instead of:
u = User.first(:login => login)
...you do something like:
u = self.login
Thanks!
The first
helper method locates the first record in your database that matches the specified criteria. It's semantically equivalent to the following SQL statement:
SELECT * FROM Users WHERE login = 'foo' LIMIT 1
The code after u = User.first(:login => login)
does the following:
1. Checks to see if there is a user returned with the specified login
2. Returns the User object if the passwords match.
u = User.first(:login => login)
means "go to the database, and find me the first User object whose login equals the userid passed in as the "login" parameter. Assign this to u.
Because you're looking for the corresponding user object, not just the login name.
精彩评论