Using `.exists?` instead of `.present?`

.exists? vs .present?

Checkout this PR I just made. You should use .exists instead of .present? if you just want to know that a record “exists” and don’t care about the value because under the hood it will do a SELECT 1 query which is much quicker than returning the entire result.

Post.joins(:topic)
  .find_by(
    "topics.id = :topic_id AND topics.deleted_at IS NULL AND posts.post_number = 1 AND posts.version = 1 AND posts.created_at > :created_at",
    topic_id: SiteSetting.welcome_topic_id,
    created_at: 1.month.ago
  ).present?

becomes

Post.joins(:topic)
  .where(
    "topics.id = :topic_id AND topics.deleted_at IS NULL AND posts.post_number = 1 AND posts.version = 1 AND posts.created_at > :created_at",
    topic_id: SiteSetting.welcome_topic_id,
    created_at: 1.month.ago
  ).exists?

Also notice that I needed to change the find_by to where for .exists? to work.