ruby - Understanding a Boolean conjunction statement in Rails method -
this might trivial point, don't understand , bothers me. going through ruby on rails tutorial michael hartl, there method definition :
def create user = user.find_by(email: params[:session][:email].downcase) if user && user.authenticate(params[:session][:password]) log_in user redirect_to user else flash.now[:danger] = "invalid email/password combination" render 'new' end end
what don't understand why conjunction
user && user.authenticate(params[:session][:password])
is neccessary. surely, there no case in user.authenticate return "true" if user object empty, , hence, nil. matter of style, or 'user &&' subtle in expression?
trying part: user.authenticate(params[:session][:password])
without first knowing if object exists throw error. need make sure have user
object before try run .authenticate
on it. make sense?
Comments
Post a Comment