# Help with pagination

**URL:** <https://www.ruby-forum.com/t/help-with-pagination/105450>\
**Category:** Rails\
**Created:** [May 30, 2007, 1:51am UTC](https://www.ruby-forum.com/t/help-with-pagination/105450 "2007-05-30T01:51:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 1:51am UTC](https://www.ruby-forum.com/t/help-with-pagination/105450/1 "2007-05-30T01:51:02Z")

</div>

I’m trying to follow the Agile Web D. example, and do  
pagination.

I have a model SearchTerm, and another model Listing

Here’s SearchTerm

class SearchTerm \< ActiveRecord::Base  
has\_many :listings  
end

In my admin controller, if I do this

def show  
@search\_term = SearchTerm.find(params[:id])  
end

Then, with a given id related to a search term, in a view, I can  
iterate through listings that are related to a given search term id,  
like this:

\<% @search\_term.listings.undeleted.each do |listing| %\>

\<%= listing.title %\>
\<% end %\>

But I can’t figure out how to paginate these listings. The Agile Web  
Development example uses one table with no parent table. If I do this  
in my admin controller:

def list  
@listing\_pages, @listings = paginate :listings, :per\_page =\> 25  
end

I can have a list.rhtml view, with this

\<% for listing in @listings %\>  
\<%= listing.title %\>  
\<% end %\>

Then if I navigate to [http://0.0.0.0:3000/admin/list](http://0.0.0.0:3000/admin/list)

I certainly get a paginated list of 25 listings, but of course they  
are not related to a given search term id, in my search\_terms table.  
It just starts with the first listing in the listings table, and gives  
the first 25.

How do I hook up pagination to the parent table?

Charlie

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 1:59am UTC](https://www.ruby-forum.com/t/help-with-pagination/105450/2 "2007-05-30T01:59:08Z")

</div>

removed\_email\_address@domain.invalid wrote:

> How do I hook up pagination to the parent table?
> 
> Charlie

def list  
@search\_term = SearchTerm.find(params[:id])  
@listing\_pages, @listings = paginate :listings, { :per\_page =\> 25,  
:conditions =\> [‘search\_term\_id = ?’, @search\_term.id] }  
end

That should do the trick. You can check out other options in the API  
docs…  
[http://api.rubyonrails.org/classes/ActionController/Pagination.html#M000130](http://api.rubyonrails.org/classes/ActionController/Pagination.html#M000130)

You can of course ommit the whole SearchTerm.find step, and put the  
params[:id] straight into the :conditions option.

A lot of people will tell you the default pagination is a little slow in  
the performance stakes, I tend to agree. Eventually you will find  
yourself rolling your own, it works out a lot better in the long run.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 2:03am UTC](https://www.ruby-forum.com/t/help-with-pagination/105450/3 "2007-05-30T02:03:03Z")

</div>

Works, thanks!

On May 29, 4:59 pm, Douglas S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 30, 2007, 5:36am UTC](https://www.ruby-forum.com/t/help-with-pagination/105450/4 "2007-05-30T05:36:16Z")

</div>

Charlie,

I posted a long how-to-paginate article that might help you.

[http://www.nullislove.com/2007/05/24/pagination-in-rails/](http://www.nullislove.com/2007/05/24/pagination-in-rails/)

Best,  
Kevin S.
