Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to make sure I'm using them for the correct occasion and want to know of any subtleties. They seem to function the same way, which is to check to see if a object field has been defined, when I use them via the console and there isn't a whole lot information online when I did a google search. Thanks!

like image 419
perseverance Avatar asked Nov 01 '12 22:11

perseverance


People also ask

Is present in Ruby?

Chromium (Cr) metal is present in Ruby as an impurity. Ruby has pink to blood-red colour. It is a gemstone. It is a variety of the mineral corundum (aluminium oxide).

What is blank Ruby?

class Object # An object is blank if it's false, empty, or a whitespace string. # For example, +false+, '', ' ', +nil+, [], and {} are all blank. # # This simplifies # # ! address || address.empty? # #

What is nil in Ruby on Rails?

In Ruby, nil is a special value that denotes the absence of any value. Nil is an object of NilClass. nil is Ruby's way of referring to nothing or void.

Is Everything an object in Ruby?

You’re calling the ‘+’ method on the ‘2’ object, and passing the other ‘2’ as an argument. So, even when expressing such a simple idea, Ruby still keeps loyal to its object-oriented ideas. Everything is an object. But what about nothing?

What is the present method in Ruby?

This one, like the previous method, isn’t native to the Ruby language itself, but provided by the Rails framework. This method is, by far, the easiest of all to understand. Here it goes: “present?” is just the negation of “blank?”.

What is the use of exponent and operator in Ruby?

Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code. For example − This may be more quickly declared using parallel assignment −

What is the difference between modulus and exponent in Ruby?

Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand. Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. Ruby also supports the parallel assignment of variables.


2 Answers

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 
like image 102
pje Avatar answered Sep 28 '22 18:09

pje


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']] 
like image 41
avital Avatar answered Sep 28 '22 17:09

avital