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

The big difference between the two methods, is that when you call present? it initializes ActiveRecord for each record found(!), while exists? does not

to show this I added after_initialize on User. it prints: 'You have initialized an object!'

User.where(name: 'mike').present?

User Load (8.1ms) SELECT "users".* FROM "users" WHERE "users"."name" = $1 ORDER BY users.id ASC  [["name", 'mike']]
You have initialized an object!
You have initialized an object!

User.exists?(name: 'mike')

User Exists (2.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."name" = $1 ORDER BY users.id ASC LIMIT 1  [["name", 'mike']]

There is a huge difference in performance, and .present? can be up to 10x slower then .exists? depending on the relation you are checking.

This article benchmarks .present? vs .any? vs .exists? and explains why they go from slower to faster, in this order.

In a nutshell, .present? (900ms in the example) will load all records returned, .any? (100ms in the example) will use a SQLCount to see if it's > 0 and .exists? (1ms in the example) is the smart kid that uses SQL LIMIT 1 to just check if there's at least one record, without loading them all neither counting them all.


SELECT COUNT(*) would scan the records to get a count.

SELECT 1 would stop after the first match, so their exec time would be very different.


The SQL generated by the two are also different.

present?:

Thing.where(name: "Bob").present?
# => SELECT COUNT(*) FROM things WHERE things.name = "Bob";

exists?:

Thing.exists?(name: "Bob")
# => SELECT 1 AS one from things WHERE name ="Bob" limit 1;

They both seem to run the same speed, but may vary given your situation.