As I''ve been working with Ruby on Rails (and PHP on Rails) there have
been several instances when I would like to be able to paginate a
collection that is implicitly retrieved for me (i.e. in an association
rather than via the "find_all" method).  I would like to see this kind
of simplicity, for example:
class Genre < ActiveRecord::Base
  has_many :movies
end
class Movies < ActiveRecord::Base
  belongs_to :genre
  paginator_defaults :per_page => 10
end
class GenresController < ApplicationController
  def show
    @genre = Genre.find(@params[''id''])
    @genre.paginator_for_movies :page => @params[''page'']
  end
end
And in the view (.rhtml):
<% for @movie in @genre.page_of_movies %>
  <%= @movie.title %><br>
<% end %>
With the benefit of this simplicity, it seems that making the model
take on a little bit more of the responsibility for pagination would
be worthwhile.  Both kinds of collections can potentially need
pagination--those retrieved by find_all, as well as those retrieved
through the model''s associations.  Therefore, tieing it to the model
is a way to reduce repetition throughout the controllers.
As of right now, it appears that I would have to manually re-construct
the has_many relationship that the model already knows about by doing
the following in the controller:
@movie_pages, @movies_in_this_genre = paginate :movies, :join => "LEFT
JOIN genres ON genres.id = movies.genre_id"
This seems to violate the DRY principle, and also makes defaults for a
particular paginator hard to maintain (e.g. if I have to set :per_page
or :order in each call to paginate when one default in the model would
do).
Is there an easier (DRY) way to do this already implemented that I''m
not seeing, or is this something that we could try to improve?  I''m
curious to see if anyone has another way to make this easier while
keeping the model paginator-free.
Duane Johnson
(canadaduane)