I am very new to the Ruby language and Ruby on Rails, so I'm not entirely sure how to word my question (and will probably use incorrect terminology). The following code does not work
@user = User.find_by email: params[:session][:email]
if @user.authenticate(params[:session][:password])
# Signin @user
else
# Render failed Signin
end
I get an error that the method authenticate is not defined for nil:Nilclass, but the following code works fine:
@user = User.find_by email: params[:session][:email]
if @user && @user.authenticate(params[:session][:password])
# Signin @user
else
# Render failed Signin
end
I don't understand why the first code block doesn't work. When I define @user does the User.find_by method not run and set the value of @user? Or does the variables value get set only when called in the program?
EDIT: unnecessary parentheses removed.