On 7 August 2010 15:46, [email protected] <[email protected]> wrote: > 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?
(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] http://www.ruby-doc.org/core/classes/Array.html#M002191 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

