Forum: Ruby on Rails help with pagination

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
on 2007-05-30 03:51
(Received via mailing list)
I'm trying to follow the Agile Web D. example, and do

I have a model SearchTerm, and another model Listing

Here's SearchTerm

class SearchTerm < ActiveRecord::Base
  has_many :listings

In my admin controller, if I do this

  def show
    @search_term = SearchTerm.find(params[:id])

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| %>
<div><%= listing.title %></div>
<% 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

I can have a list.rhtml view, with this

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

Then if I navigate to

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?

Douglas S. (Guest)
on 2007-05-30 03:59
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 = ?', ] }

That should do the trick. You can check out other options in the API

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.
on 2007-05-30 04:03
(Received via mailing list)
Works, thanks!

On May 29, 4:59 pm, Douglas S. <removed_email_address@domain.invalid>
Kevin S. (Guest)
on 2007-05-30 07:36
(Received via mailing list)

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

Kevin S.
This topic is locked and can not be replied to.