# Paginate and getting count of total number of records

**URL:** <https://www.ruby-forum.com/t/paginate-and-getting-count-of-total-number-of-records/69852>\
**Category:** Rails\
**Created:** [September 25, 2006, 12:54am UTC](https://www.ruby-forum.com/t/paginate-and-getting-count-of-total-number-of-records/69852 "2006-09-25T00:54:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [September 25, 2006, 12:54am UTC](https://www.ruby-forum.com/t/paginate-and-getting-count-of-total-number-of-records/69852/1 "2006-09-25T00:54:50Z")

</div>

I am using paginate to get the list of events from my database. Besides  
pagination I also want to display the total number of records.

I noticed that the paginate query inside does do a SQL query to get the  
total numbe of records. Is there a way to tap that count rather than  
having  
a separate query to just to get the count of records.

My existing pagination query is given below.

```
@query_join = "JOIN venues ON venues.id = events.venue_id "
@query_join << "JOIN cities ON cities.id = venues.city_id"
@event_pages, @events = paginate :events,
:joins => @query_join,
:conditions => ["start_date > ? ",Time.now],
:per_page => per_page,
:order => :start_date,
:select => "events.*"

```

The generate query in the log:

SELECT count(\*) AS count\_all FROM events JOIN venues ON venues.id =  
events.venue\_id JOIN cities ON cities.id = venues.city\_id WHERE  
(start\_date

> ‘2006-09-25 04:01:17’ )

SELECT events.\* FROM events JOIN venues ON venues.id = events.venue\_id  
JOIN  
cities ON cities.id = venues.city\_id WHERE (start\_date \> ‘2006-09-25  
04:01:17’ ) ORDER BY start\_date LIMIT 0, 10

Thanks.  
-=-

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [September 25, 2006, 6:43am UTC](https://www.ruby-forum.com/t/paginate-and-getting-count-of-total-number-of-records/69852/2 "2006-09-25T06:43:22Z")

</div>

On Sep 24, 2006, at 4:49 PM, Neeraj K. wrote:

> noticed that the paginate query inside does do a SQL query to get  
> the total numbe of records. Is there a way to tap that count rather  
> than having a separate query to just to get the count of records.

@event\_pages.item\_count

Aaron

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [September 25, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/paginate-and-getting-count-of-total-number-of-records/69852/3 "2006-09-25T14:32:11Z")

</div>

Thanks
