Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between `any?` and `exists?` in Ruby on Rails?

In Ruby on Rails, there appear to be two methods to check whether a collection has any elements in it.

Namely, they are ActiveRecord::FinderMethods’ exists? and ActiveRecord::Relation’s any?. Running these in a generic query (Foo.first.bars.exists? and Foo.first.bars.any?) generated equivalent SQL. Is there any reason to use one over the other?

like image 540
J3RN Avatar asked Jan 19 '18 22:01

J3RN


People also ask

What is the difference between find and Find_by in rails?

The additional difference between find() and find_by() is that find could only be used to search by primary key (usually the 'id') while the find_by() requires and searches by attribute (either passed as hash like Employee. find_by(name: 'Mike') or using the Employee.

What does .save do in Ruby?

The purpose of this distinction is that with save! , you are able to catch errors in your controller using the standard ruby facilities for doing so, while save enables you to do the same using standard if-clauses.

Is Ruby on Rails a database?

Rails comes with built-in support for SQLite, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.

What is the difference between Ruby and Ruby on rails?

The main difference between Ruby and Ruby on Rails is their nature. Ruby is a programming language used to develop desktop and web applications, while Ruby on Rails is a framework that supports Ruby programming along with several more programming and markup languages. Ruby offers several features, such as DRY (Don’t Repeat Yourself).

What is the future of Ruby on rails in web development?

You can utilize Ruby on Rails dramatically in different fields. Owing to the constant updates and stable release 6.0.3.1, RoR portrays a bright future. Ruby on Rails development companies empower their web applications, which is beneficial for both, start-ups and reputable enterprises.

When to use find_by vs where in rails?

If you’re expecting one record (a specific user), use find_by, for multiple records (a list of users) use where. But where has many ways to use it, which often confuses beginners. No problem! We’re going to take a look at the different ways you can use where in your Rails applications.

How to query the database through models in rails?

In Rails, you can query the database through your models to access your data. You can do this using ActiveRecord methods. Like where, find, or find_by. As a result you get:


2 Answers

#any and #exists? are very different beasts but query similarly.

Mainly, #any? accepts a block — and with this block it retrieves the records in the relation, calls #to_a, calls the block, and then hits it with Enumerable#any?. Without a block, it's the equivalent to !empty? and counts the records of the relation.

#exists? always queries the database and never relies on preloaded records, and sets a LIMIT of 1. It's much more performant vs #any?. #exists? also accepts an options param as conditions to apply as you can see in the docs.

like image 187
Josh Brody Avatar answered Sep 22 '22 11:09

Josh Brody


The answers here are all based on very outdated versions. This commit from 2016 / ActiveRecord 5.1 changes empty?, which is called by any? when no block is passed, to call exists? when not preloaded. So in vaguely-modern Rails, the only difference when no block is passed is a few extra method calls and negations, and ignoring preloaded results.

like image 28
Adam Avatar answered Sep 26 '22 11:09

Adam