# Shouldn't XXX.all mean no more queries?

**URL:** <https://www.ruby-forum.com/t/shouldnt-xxx-all-mean-no-more-queries/192885>\
**Category:** Rails\
**Created:** [August 7, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/shouldnt-xxx-all-mean-no-more-queries/192885 "2010-08-07T16:47:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 7, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/shouldnt-xxx-all-mean-no-more-queries/192885/1 "2010-08-07T16:47:39Z")

</div>

Hey

I thought if I did “Pages.all” in the controller that would load all  
rows?

Since, in my view if I later do “Pages.find(1)” that causes another  
query… ?

I did try doing “@pages = Pages.all” and then querying  
“@pages.find(1)” … but no luck

any ideas?

cheers

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 7, 2010, 5:15pm UTC](https://www.ruby-forum.com/t/shouldnt-xxx-all-mean-no-more-queries/192885/2 "2010-08-07T17:15:07Z")

</div>

> Since, in my view if I later do “Pages.find(1)” that causes another  
> query… ?

That’s normal behavior.

You can’t expect Rails to be clever enough to read into your mind that  
way. You could write your own method to search for a Page with id of 1  
inside your array.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 7, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/shouldnt-xxx-all-mean-no-more-queries/192885/3 "2010-08-07T17:30:23Z")

</div>

On 7 August 2010 15:46, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> any ideas?

(Assuming you’re on Rails 2:)

Essentially, when you call a finder method (e.g. .all or .find),  
ActiveRecord executes the SQL, turns the result into ActiveRecord  
objects, and hands them back. That’s all. It doesn’t store the results  
internally. So when you call another finder method, it’ll just do the  
same thing for that method, including hitting the database again.

There is one special exception to this, which is the ‘query cache’.  
This caches the results of a particular SQL SELECT query, and if you  
run that _exact same_ SQL SELECT query again then it’ll return the  
same results to you without hitting the database again. But that’s all  
it does: it just caches the results of one particular SQL query; it’s  
not smart enough to figure out that Page.all and Page.find(1) are  
going to contain the same row.

In your case, Page.all returns a normal Ruby array, so you could use  
normal Ruby methods like Array#select [1] to pull out objects directly  
from that array, without hitting the database again. That’s not the  
most elegant thing in the world, though.

Hope that helps,  
Chris

[1] [class Array - RDoc Documentation](http://www.ruby-doc.org/core/classes/Array.html#M002191)
