<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -198,8 +198,8 @@ module WillPaginate
         find_options[:select] = &quot;COUNT(*), #{group_by}&quot;
         find_options[:group] = group_by
 
-        sql = construct_finder_sql(find_options)
-        results = connection.execute(sql)
+        results = wp_query_counts_by_group find_options
+        
         total = 0
         links = results.map do |r| 
           page = (total / per_page) + 1
@@ -209,6 +209,32 @@ module WillPaginate
         [links, total]
       end
 
+      # Generate a query against the database which includes the &quot;COUNT(*)&quot; in the SELECT. This is a little
+      # tricky because we have to support the case where you're paginating through an association. Unfortunately,
+      # the standard ActiveRecord code doesn't support that so we end up generating the correct SQL here.
+      def wp_query_counts_by_group(options)
+        scope = scope(:find)
+        if options.has_key? :include
+          join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
+          sql = &quot;SELECT #{options[:select]} FROM #{(scope &amp;&amp; scope[:from]) || options[:from] || quoted_table_name} &quot;
+          sql &lt;&lt; join_dependency.join_associations.collect{|join| join.association_join }.join
+          add_joins!(sql, options, scope)
+          add_conditions!(sql, options[:conditions], scope)
+          add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) &amp;&amp; ((scope &amp;&amp; scope[:limit]) || options[:limit])
+
+          add_group!(sql, options[:group], scope)
+          add_order!(sql, options[:order], scope)
+          add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
+          add_lock!(sql, options, scope)
+
+          sql = sanitize_sql(sql)
+        else
+          sql = construct_finder_sql(options)
+        end
+
+        connection.execute(sql)
+      end
+
       # Does the not-so-trivial job of finding out the total number of entries
       # in the database. It relies on the ActiveRecord +count+ method.
       def wp_count(options, args, finder)</diff>
      <filename>lib/will_paginate/finder.rb</filename>
    </modified>
    <modified>
      <diff>@@ -277,6 +277,24 @@ class FinderTest &lt; ActiveRecordTestCase
     end
   end
   
+  def test_group_by_associations_with_include
+    project = projects(:active_record)
+
+    entries = project.topics.paginate \
+      :page     =&gt; 1, 
+      :include  =&gt; :replies,  
+      :conditions =&gt; &quot;replies.content LIKE 'Nice%' &quot;, 
+      :per_page =&gt; 10,
+      :group_by =&gt; &quot;replies.content&quot;
+
+    expected = Topic.find :all, 
+      :include =&gt; 'replies', 
+      :conditions =&gt; &quot;project_id = #{project.id} AND replies.content LIKE 'Nice%' &quot;, 
+      :limit   =&gt; 10
+
+    assert_equal expected, entries.to_a
+  end
+
   ## misc ##
 
   def test_count_and_total_entries_options_are_mutually_exclusive</diff>
      <filename>test/finder_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>d5d509671f403c224c193260fef72f85e3d433d7</id>
    </parent>
  </parents>
  <author>
    <name>Denis Hennessy</name>
    <email>denis@hennessynet.com</email>
  </author>
  <url>http://github.com/dhennessy/will_paginate/commit/f19f130d4adbbbaff184740ed61eb77678bf1df1</url>
  <id>f19f130d4adbbbaff184740ed61eb77678bf1df1</id>
  <committed-date>2008-09-24T07:46:01-07:00</committed-date>
  <authored-date>2008-09-24T07:46:01-07:00</authored-date>
  <message>Add support for grouped pagination through an association.

This changeset extends the code to correctly generate the initial SQL query (to find the number of
records in each grouping bucket) when the list you're paging is actually an association.</message>
  <tree>186ac693cd252ac20057c3a4ac0a06e1671764ed</tree>
  <committer>
    <name>Denis Hennessy</name>
    <email>denis@hennessynet.com</email>
  </committer>
</commit>
