Hey guys. I want to create a overview page for my application that contains the latest activity on the site, from a few different sources. Lets say we have messages, comments, files and tasks (much like Basecamp, if you''ve ever used it) and you want to display them all in one big list, intermixed, and sorted by date. I know I could run queries on all the separate models and then combine the results, but because this will be paginated, I would end up getting more records than I really need, and having to trash some, that seems wasteful and and potentially a hit to performance. So, I''d really like to be able to put a LIMIT on the DB query itself, as well, if possible. What do you think is the best way to go about this? Thanks a lot guys! -- 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.
Marnen Laibow-Koser
2010-Feb-01 22:06 UTC
Re: Query multiple models/tables for an "Overview" page
Seirie wrote:> Hey guys. I want to create a overview page for my application that > contains the latest activity on the site, from a few different > sources. Lets say we have messages, comments, files and tasks (much > like Basecamp, if you''ve ever used it) and you want to display them > all in one big list, intermixed, and sorted by date. > > I know I could run queries on all the separate models and then combine > the results, but because this will be paginated, I would end up > getting more records than I really need, and having to trash some, > that seems wasteful and and potentially a hit to performance. So, I''d > really like to be able to put a LIMIT on the DB query itself, as well, > if possible.If you''re querying multiple tables, then there is no way to do this. If you''re going to display a total of (say) 30 records, I think you''ll just have to do LIMIT 30 on each table and discard the rest. You could do some join trickery to get around this, but I suspect that AR wouldn''t recognize the results.> > What do you think is the best way to go about this? Thanks a lot guys!Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hmm, alright, I suspected that might be the case. Suppose it''s not such a big loss. Thanks Marnen! On Feb 1, 5:06 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Seirie wrote: > > Hey guys. I want to create a overview page for my application that > > contains the latest activity on the site, from a few different > > sources. Lets say we have messages, comments, files and tasks (much > > like Basecamp, if you''ve ever used it) and you want to display them > > all in one big list, intermixed, and sorted by date. > > > I know I could run queries on all the separate models and then combine > > the results, but because this will be paginated, I would end up > > getting more records than I really need, and having to trash some, > > that seems wasteful and and potentially a hit to performance. So, I''d > > really like to be able to put a LIMIT on the DB query itself, as well, > > if possible. > > If you''re querying multiple tables, then there is no way to do this. If > you''re going to display a total of (say) 30 records, I think you''ll just > have to do LIMIT 30 on each table and discard the rest. > > You could do some join trickery to get around this, but I suspect that > AR wouldn''t recognize the results. > > > > > What do you think is the best way to go about this? Thanks a lot guys! > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.