rails.nerd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Aug-07 14:46 UTC
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 7 August 2010 15:46, rails.nerd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <rails.nerd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.