Reputation: 653
I have a database object in active record. If I call object.find(1).present?
it returns false
, but it exists. Calling !object.find(1).nil?
returns true
.
Why is this? I thought !nil == present?
.
Upvotes: 1
Views: 4362
Reputation: 1085
To better answer your question lets look at the implementation:
def present?
!blank?
end
We don't see nil?
mentioned here, just blank?
, so lets check out blank?
as well:
def blank?
respond_to?(:empty?) ? !!empty? : !self
end
So essentially, if the object responds_to
empty?
it will call out to that for the result. Objects which have an empty?
method include Array, Hash, String, and Set.
Further Reading
Upvotes: 5
Reputation: 239230
nil?
and present?
are not opposites.
Many things are both not present?
and not nil?
, such as an empty string or empty array.
"".present? # => false
"".nil? # => false
[].present? # => false
[].nil? # => false
Upvotes: 5