When you call methods on objects you have to know what you are doing.
current_user.present?
Here you call a method called present?
on the object current_user
. This works because method present?
is defined on this object. When you do
current_user.exists?
you expect current_user
to respond to a method called exists?
. But it does not, thus error.
You mixed up a few things into this single question.
Only call methods on an object if you are sure it responds to this method.
Difference between if current_user
vs if current_user.present?
is implicit vs explicit check for object truthiness. See, in Ruby, everything except for false
and nil
is truthy. So if current_user
means if current_user
is anything except for nil
or false
then proceed. You rely on expression evaluation, while in current_user.present?
you rely explicitly on a return value from a method call (present?
).
I suggest you
- always go with explicit because it reads better;
- read about objects and methods in Ruby.