Shouldn't XXX.all mean no more queries?

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

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.

(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 (Ruby 3.1.2)