Find, paginate, conditions and associations

Hi, I’m trying to figure out how to use paginate when one of the
conditions depends on the association between one model and another.

For example: I have a model Authors which has_many Posts. I would like
to find all the authors and order them by the number of posts that they
have published and then paginate those results.

What if I wanted to base the paginate condition on all the posts with a
specific word in the title and grouped by the author?

Thanks in advance!

Scott

What I would do in these cases is to first create an instance variable
containing the results of the search based on your initial find
conditions, then paginate on that result set with ordering and any
conditions that are based on the collection rather than each record.

So in your first example I would first do an SQL query to get the count
of Posts for each Author and store the result set in an instance
variable, say @myauthors. Then paginate on @myauthors ordered by the
count of posts. You can add more conditions at this point, such as only
authors who made at least 5 posts.

Your second example isn’t quite clear to me. If you mean you want to
only count posts in the first example that have a particular word in
them, you can add that condition to the initial SQL query. If you mean
it as a separate example, just to paginate through all the posts without
displaying the counts, you can do that by first getting a result set
with all the posts with the specific word in their title and the names
of their authors, then paginating on that result set ordered by author’s
name.

Hope this helps,
Shauna

Hi Scott,

First of all i must tell you not to use Paginate with conditions, it is
known to be very overloading and sometimes can even bring you to a
freeze.
Use paginate_by_sql plugin instead, this will also solve your problem
with the relation sorting