Skip to content

Instantly share code, notes, and snippets.

@ethangunderson
Created October 29, 2015 15:21
Show Gist options
  • Save ethangunderson/26e3b0333ff4f55ba148 to your computer and use it in GitHub Desktop.
Save ethangunderson/26e3b0333ff4f55ba148 to your computer and use it in GitHub Desktop.
Friendly reminder that exists?, present? and any? behave differently than what you might expect.
irb(main):001:0> User.exists?
users Columns (20.3ms) SHOW FIELDS FROM `users`
User Exists (0.3ms) SELECT 1 AS one FROM `users` LIMIT 1
=> true
irb(main):002:0> User.where(:email => "foo").exists?
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'foo' LIMIT 1
=> false
irb(main):003:0> User.where(:email => "foo").present?
(0.7ms) SELECT COUNT(*) FROM `users` WHERE `users`.`email` = 'foo'
=> false
irb(main):004:0> User.where(:email => "foo").any?
(0.5ms) SELECT COUNT(*) FROM `users` WHERE `users`.`email` = 'foo'
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment