What is the difference between using .exists?, and .present? in Ruby?

To clarify: neither present? nor exists? are “pure” ruby—they’re both from Rails-land.

present?

present? is an ActiveSupport extension to Object. It’s usually used as a test for an object’s general “falsiness”. From the documentation:

An object is present if it’s not blank?. An object is blank if it’s false, empty, or a whitespace string.

So, for example:

[ "", " ", false, nil, [], {} ].any?(&:present?)
# => false

exists?

exists? is from ActiveResource. From its documentation:

Asserts the existence of a resource, returning true if the resource is found.

Note.create(:title => 'Hello, world.', :body => 'Nothing more for now...')
Note.exists?(1) # => true

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)