# Query multiple models/tables for an "Overview" page

**URL:** <https://www.ruby-forum.com/t/query-multiple-models-tables-for-an-overview-page/182720>\
**Category:** Rails\
**Created:** [February 1, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/query-multiple-models-tables-for-an-overview-page/182720 "2010-02-01T22:45:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 1, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/query-multiple-models-tables-for-an-overview-page/182720/1 "2010-02-01T22:45:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 1, 2010, 11:06pm UTC](https://www.ruby-forum.com/t/query-multiple-models-tables-for-an-overview-page/182720/2 "2010-02-01T23:06:26Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Seirie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@Seirie](https://www.ruby-forum.com/u/Seirie)\
**Post date:** [February 2, 2010, 12:00am UTC](https://www.ruby-forum.com/t/query-multiple-models-tables-for-an-overview-page/182720/3 "2010-02-02T00:00:30Z")

</div>

Hmm, alright, I suspected that might be the case. Suppose it’s not  
such a big loss. Thanks Marnen!
