<?xml version='1.0' encoding='utf-8' ?>
<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/'>
  <channel>
    <title>Wolfmans Howlings: Paginating acts_as_taggable with will_paginate</title>
    <link>http://blog.wolfman.com/articles/2007/7/30/paginating-acts_as_taggable-with-will_paginate</link>
    <description>A programmers Blog about Ruby, Rails and a few other issue</description>
    <language>en-us</language>
    <ttl>40</ttl>
    <item>
      <title>Paginating acts_as_taggable with will_paginate</title>
      <description>
        &lt;p&gt;A question I see asked a lot is how do I paginate acts_as_taggable
        (on steroids)?&lt;/p&gt;
        
        &lt;p&gt;I haven't seen any answers I liked, so I created my own, which I'm
        sure a few people won't like either ;) But it works for me (tm).&lt;/p&gt;
        
        &lt;p&gt;I use &lt;a href=&quot;http://errtheblog.com/post/4791&quot;&gt;will_paginate&lt;/a&gt;, but this does
        not work with custom finds that plugins define themselves, as is the
        case with
        &lt;a href=&quot;http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids&quot;&gt;acts_as_taggable&lt;/a&gt;.&lt;/p&gt;
        
        &lt;p&gt;If you do use a custom find_by_sql you have to hit the database twice, once to find the total number
        of items and then the paginated find.&lt;/p&gt;
        
        &lt;p&gt;I have a situation where I generate a 
        &lt;a href=&quot;http://blog.wolfman.com/articles/2007/06/23/developing-a-social-networking-site-part-3-tag-cloud&quot;&gt;tag cloud&lt;/a&gt;
        with every page, and part of that tag cloud has already calculated the
        number of tags for each classification I use.&lt;/p&gt;
        
        &lt;p&gt;I combined this with the &lt;a href=&quot;http://paginator.rubyforge.org/&quot;&gt;paginator gem&lt;/a&gt; 
        to get myself pages without too many hits to the database.&lt;/p&gt;
        
        &lt;p&gt;The first thing I do is pass the total count I get from the tag cloud
        to the action that renders the index for all those items matching the
        cloud... If you look at the article cited above I make this
        modification, this is dumbed down a bit for the sake of simplicity...&lt;/p&gt;
        
        &lt;pre&gt;  &lt;span class=&quot;ident&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;tag_counts&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:order&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;tags.name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;')&lt;/span&gt;
          &lt;span class=&quot;ident&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;link_to&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;tagged_post_path&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:tag&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:cnt&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;This passes the count I have already calculated to the action that
        will list the paginated results.&lt;/p&gt;
        
        &lt;p&gt;In my controller I do this to get the paginated results using
        will_paginate and the Pagination gem...&lt;/p&gt;
        
        &lt;pre&gt;    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;tagged&lt;/span&gt;
            &lt;span class=&quot;punct&quot;&gt;...&lt;/span&gt;
              &lt;span class=&quot;comment&quot;&gt;# page if we can&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:cnt&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]&lt;/span&gt;
              &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;size&lt;/span&gt;
                &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;10&lt;/span&gt;
        
                &lt;span class=&quot;comment&quot;&gt;# use Paginator gem to do the actual paging&lt;/span&gt;
                &lt;span class=&quot;ident&quot;&gt;pager&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Paginator&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
                  &lt;span class=&quot;constant&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;find_tagged_with&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:limit&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:offset&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        
                &lt;span class=&quot;comment&quot;&gt;# default to page 1 if not specified&lt;/span&gt;
                &lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;1&lt;/span&gt;
        
                &lt;span class=&quot;comment&quot;&gt;# gets a paged array of posts&lt;/span&gt;
                &lt;span class=&quot;attribute&quot;&gt;@posts&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;returning&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;WillPaginate&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Collection&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
                  &lt;span class=&quot;ident&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;replace&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;pager&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;items&lt;/span&gt;
                &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
              &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
                &lt;span class=&quot;comment&quot;&gt;# fall back if we don't know the size&lt;/span&gt;
                &lt;span class=&quot;attribute&quot;&gt;@posts&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;find_tagged_with&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;])&lt;/span&gt;
              &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        
              &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:action&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;    
        &lt;/pre&gt;
        
        &lt;p&gt;This fits in nicely with the tag cloud I need to calculate, and it
        uses will_paginate just like the regular index action does.&lt;/p&gt;
        
        &lt;p&gt;&lt;em&gt;UPDATE&lt;/em&gt;&lt;/p&gt;
        
        &lt;p&gt;I refactored this to be more generally useful, I added the following as a
        protected method in &lt;code&gt;application.rb&lt;/code&gt;...&lt;/p&gt;
        
        &lt;pre&gt;  &lt;span class=&quot;comment&quot;&gt;# paginate a call to find_tagged_with&lt;/span&gt;
          &lt;span class=&quot;comment&quot;&gt;# klass is the tagged class&lt;/span&gt;
          &lt;span class=&quot;comment&quot;&gt;# tag is the tag to find&lt;/span&gt;
          &lt;span class=&quot;comment&quot;&gt;# count is the total number of items with that tag, if nil count_tags is called&lt;/span&gt;
          &lt;span class=&quot;comment&quot;&gt;# per_page is numbe rof items per page&lt;/span&gt;
          &lt;span class=&quot;comment&quot;&gt;# page is the page we are on&lt;/span&gt;
          &lt;span class=&quot;comment&quot;&gt;# order is the order to return the items in&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;tag_paginator&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;klass&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;='&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;updated_at DESC&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;')&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;klass&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;count_tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;pager&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Paginator&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;klass&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;find_tagged_with&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:order&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:limit&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:offset&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        
            &lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;1&lt;/span&gt;
        
            &lt;span class=&quot;ident&quot;&gt;returning&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;WillPaginate&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Collection&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
              &lt;span class=&quot;ident&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;replace&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;pager&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;items&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;I call it from one of my other actions like this...&lt;/p&gt;
        
        &lt;pre&gt;&lt;code&gt;@faqs= tag_paginator(Post, 'FAQ', nil, per_page, params[:page], 'updated_at DESC')
        &lt;/code&gt;&lt;/pre&gt;
        
        &lt;p&gt;Passing in nil as the third parameter causes the &lt;code&gt;tag_paginator&lt;/code&gt;
        method to call &lt;code&gt;Post.count_tags&lt;/code&gt; which is not part of the
        &lt;code&gt;acts_as_taggable&lt;/code&gt; methods, I added it to the SingletonMethods module
        myself...&lt;/p&gt;
        
        &lt;pre&gt;&lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;ActiveRecord&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;Acts&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;#:nodoc:&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;Taggable&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;#:nodoc:&lt;/span&gt;
              &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;SingletonMethods&lt;/span&gt;
        
              &lt;span class=&quot;punct&quot;&gt;...&lt;/span&gt;
        
                &lt;span class=&quot;comment&quot;&gt;# Return the count of tag tags in this class&lt;/span&gt;
               &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;count_tags&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
                 &lt;span class=&quot;ident&quot;&gt;count_by_sql&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;select count(*) FROM tags, taggings WHERE &lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;sanitize_sql&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(['&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;name = ? AND tags.id = taggings.tag_id AND taggable_type = ?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;',&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]))&lt;/span&gt;
               &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        
              &lt;span class=&quot;punct&quot;&gt;....&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;If you don't want to hack &lt;code&gt;acts_as_taggable&lt;/code&gt; then simply leave that
        part out and call the &lt;code&gt;count_by_sql&lt;/code&gt; yourself.&lt;/p&gt;
        
        &lt;p&gt;My refactored tagged action from above now looks like this...&lt;/p&gt;
        
        &lt;pre&gt;&lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;tagged&lt;/span&gt;
          &lt;span class=&quot;punct&quot;&gt;...&lt;/span&gt;
          &lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]&lt;/span&gt;
          &lt;span class=&quot;ident&quot;&gt;per_page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;10&lt;/span&gt;
          &lt;span class=&quot;ident&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:cnt&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]&lt;/span&gt;
          &lt;span class=&quot;attribute&quot;&gt;@posts&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;tag_paginator&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:page&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;])&lt;/span&gt;
        &lt;/pre&gt;
        
        &lt;p&gt;&lt;em&gt;UPDATE&lt;/em&gt;
        Johns suggestion works as of today (2/12/2009) so none of the above is needed...&lt;/p&gt;
        
        &lt;pre&gt;&lt;code&gt;  options = Car.find_options_for_tagged_with(params[:tag_name]).merge :page =&amp;gt; params[:page] @cars = Car.paginate(options)
        &lt;/code&gt;&lt;/pre&gt;
        
        &lt;p&gt;or my example from above...&lt;/p&gt;
        
        &lt;pre&gt;&lt;code&gt; opts= Faq.find_options_for_tagged_with('FAQ')
         @faqs= Faq.paginator(opts.merge(:page =&amp;gt; params[:page]))
        &lt;/code&gt;&lt;/pre&gt;
        
        &lt;p&gt;&lt;a href=&quot;http://technorati.com/tag/acts_as_taggable+will_paginate&quot; rel=&quot;tag&quot;&gt;&lt;/a&gt;&lt;/p&gt;
      </description>
      <author>Jim Morris</author>
      <pubDate>Thu, 12 Feb 2009 15:28:58 -0800</pubDate>
      <link>http://blog.wolfman.com/articles/2007/7/30/paginating-acts_as_taggable-with-will_paginate</link>
      <guid isPermaLink='false'>urn:uuid:e7b7bbf9-0707-48d6-a1ed-7603966b049b</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Eric</title>
      <description>
        I found this working with `acts_as_taggable` and `will_paginate`.
        
        `@events = Event.paginate_by_id(Event.find_tagged_with(params[:id]), :page =&gt; params[:page])`
        
        Maybe there is a more graceful way.
      </description>
      <pubDate>Wed, 15 Aug 2007 02:30:32 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-161</link>
      <guid isPermaLink='false'>urn:uuid:75687cf3-2b35-43da-aa2e-c363cf17f8aa</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by John Wong</title>
      <description>
        Hey man....   seriously ..  u dont have to put so much work just to get it to work.
        Just use `acts_as_taggable_on_sterioids` ...
        
        And use 2 simple lines
        
             options = Car.find_options_for_tagged_with(params[:tag_name]).merge :page =&gt; params[:page]
            @cars = Car.paginate(options)
      </description>
      <pubDate>Wed, 12 Sep 2007 01:51:22 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-162</link>
      <guid isPermaLink='false'>urn:uuid:bfb37300-1f87-440d-9102-b078e75d50ce</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Dave</title>
      <description>This works unless you have `:match_all` =&gt; true in there.  Once you do that, a group by is added that causes `will_paginate` to get a MySQL syntax error.  Still working on a way to fix it...</description>
      <pubDate>Fri, 19 Oct 2007 17:03:59 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-163</link>
      <guid isPermaLink='false'>urn:uuid:c8b84d06-e5bf-463f-af5d-bdecf2370c66</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Aaron H.</title>
      <description>
        Dave,
        I found this article while having the same problem.  
        
        The SQL error when using :match\_all =&gt; true has to do with the will\_paginate trying to count via the select created by acts\_as\_taggable.  My solution was to add the following line to the beginning of the wp\_count! function in finder.rb of will\_paginate:
        
        `options[:select].sub!(/\.\*/, '\1.id') if options[:select] #count by id's, not wildcard (\*)`
        
        This will work unless the table you are counting does not have an id column, but using active record that should be extremely rare. If that's the case, you could always have it find the name of the class's key field.
      </description>
      <pubDate>Mon, 26 Nov 2007 18:09:51 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-164</link>
      <guid isPermaLink='false'>urn:uuid:9b451d70-f269-415c-abf0-d28a08eb4f53</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by wolfmanjm</title>
      <description>the match_all option doesn't work on postgresql anyway, I found a different workaround that uses a subselect to find the ids, then looks up using IN (ids,..) if anyone needs it let me know and I'll post it.</description>
      <pubDate>Tue, 27 Nov 2007 18:17:06 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-165</link>
      <guid isPermaLink='false'>urn:uuid:46265190-a714-4016-98af-d64131ebfea3</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by goodwill</title>
      <description>
        I tried John Wong's comment but seems not working... (Dave u are referring to this right?) 
        
        I really think if such way would work its very clean.
      </description>
      <pubDate>Sat, 05 Jan 2008 01:37:06 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-166</link>
      <guid isPermaLink='false'>urn:uuid:544ddace-0b5c-4c17-a37d-b947359bacf6</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Henry</title>
      <description>i had an sql error using John's comment.  Aaron's solution seems to work though.</description>
      <pubDate>Sat, 05 Jan 2008 14:36:06 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-167</link>
      <guid isPermaLink='false'>urn:uuid:70e7be8c-0c9e-403c-9b6b-028c250530a6</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by P jam aka the nerb</title>
      <description>
        Eric, a whole rails version later and using steroids, and still your 'eloquent' code snippet works great for me.
        
        since i'm trying to keep my plugins as natural as possible, i thank you.  oh and you too dave.
      </description>
      <pubDate>Thu, 27 Mar 2008 04:34:31 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-168</link>
      <guid isPermaLink='false'>urn:uuid:1a3033ef-4ab4-468c-84ac-0eb93bd050cb</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by P jam aka the nerb</title>
      <description>errr, dave = jim, wolfman, whathaveyou.</description>
      <pubDate>Thu, 27 Mar 2008 04:39:47 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-169</link>
      <guid isPermaLink='false'>urn:uuid:6dee2089-fb3b-4981-bcbf-9ea45204210e</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by COP</title>
      <description>yes Eric's solution works for me too... one line thats it. I M LOVING RoR!!!</description>
      <pubDate>Wed, 23 Apr 2008 01:09:37 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-170</link>
      <guid isPermaLink='false'>urn:uuid:7aa53d43-c1ec-4fb8-8490-dc3a84479ff9</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by rohit /pathakorama1@gmail.com</title>
      <description>
        hi sir ,could you help me please
        i am using both the plugin will paginate and acts as taggble steroids)
        i have the model Article which is acts_as_taggable
        when i try to save the article withs tags it give me error 
        stack level too deep
        vendor/plugins/will paginate/lib/will_paginate/finder.rb:163:in `method_missing_without_paginate'
        vendor/plugins/will paginate/lib/will_paginate/finder.rb:164:in `method_missing'
        vendor/plugins/acts_as_taggable/lib/acts_as_taggable.rb:172:in `save_tags'
      </description>
      <pubDate>Tue, 30 Sep 2008 23:30:10 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-171</link>
      <guid isPermaLink='false'>urn:uuid:cf50148b-540a-4d11-9142-21184375d2c6</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Flavio Granero</title>
      <description>Hey Guys, to make the John code works, just change the method name `find_options_for_tagged_with` by `find_options_for_find_tagged_with`.</description>
      <pubDate>Wed, 12 Nov 2008 17:46:52 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-172</link>
      <guid isPermaLink='false'>urn:uuid:a4675dd8-64e2-4280-b8ae-94160e0c25b2</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by samotage</title>
      <description>
        Hey,
        
        I banged away at this for a while, then it occured to me there has to be a simpler way?
        
        So I paginated the results of the find tagged with, and bingo!
        
        @contacts = Contact.find_tagged_with(params[:tag]).paginate(:page =&gt; params[:page], :per_page =&gt; 15)
        
        Hope this helps,
        
        Sam.
      </description>
      <pubDate>Thu, 26 Feb 2009 16:58:12 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-244</link>
      <guid isPermaLink='false'>urn:uuid:8671a050-27f7-4b56-b546-f708340b98b5</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Rob</title>
      <description>@samotage - What you propose works well but judging by the SQL in my logs, it looks like it is still pulling all the records for each page? I don't see any LIMIT in the SQL statements when using .find().paginate(). In other words, on the front end, the pagination helper is giving the correct number of pages and each page is showing the correct records. But it looks like ActiveRecord is fetching the entire unpaged recordset for each page. I'm new to Ruby and Rails so maybe I'm just missing something.</description>
      <pubDate>Thu, 02 Apr 2009 12:44:57 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-245</link>
      <guid isPermaLink='false'>urn:uuid:2c3919bc-da3a-4a20-ade1-3389219dc384</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by James Rissler</title>
      <description>I found Eric's method to work best. No need to change either plug-in. Worked for me with the latest version of rails + plugins.</description>
      <pubDate>Thu, 09 Apr 2009 10:16:43 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-246</link>
      <guid isPermaLink='false'>urn:uuid:aea2e9a2-aa5d-485a-bdce-d3245b4e2b1f</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by Gudata</title>
      <description>
        Eric solution is working perfect for me 
        
        @events = Event.paginate_by_id(Event.find_tagged_with(params[:id]), :page =&gt; params[:page])
        
        10x!
      </description>
      <pubDate>Thu, 04 Jun 2009 09:45:21 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-253</link>
      <guid isPermaLink='false'>urn:uuid:cfc58e4f-6fe3-4c21-bd7c-d28652e2867b</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by 司徒正美</title>
      <description>
        Thank you,John Wong!You're a genius!
        
          def tag
            options = Lemma.find_options_for_find_tagged_with(params[:id]).merge(:page =&gt; params[:page] ||1,:per_page =&gt;3 )
            @lemmas = Lemma.paginate(options)
          end
      </description>
      <pubDate>Sun, 28 Jun 2009 21:53:12 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-256</link>
      <guid isPermaLink='false'>urn:uuid:66e7a6ba-2af3-462b-ad5f-6988737768cd</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by 司徒正美</title>
      <description>
        my platform:&lt;br/&gt;
        rails2.3.2&lt;br/&gt;
        windowxp&lt;br/&gt;
        will_paginate 2.3.11&lt;br/&gt;
        acts_as_taggable_on_steroids1.1
      </description>
      <pubDate>Sun, 28 Jun 2009 22:01:00 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-257</link>
      <guid isPermaLink='false'>urn:uuid:8475ffe8-5415-449e-88f8-f3a22f817059</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by kayen</title>
      <description>
        @   wolfmanjm  
        
        How did you get the match_all working in postgres? I'm also stuck at the same issue and would like to see your take on it!
        
        Thanks.
      </description>
      <pubDate>Sun, 06 Sep 2009 21:19:43 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-268</link>
      <guid isPermaLink='false'>urn:uuid:2ef043a0-fff0-49f0-ab3c-fcc83e4f2fbf</guid>
    </item>
    <item>
      <title>"Paginating acts_as_taggable with will_paginate" by wolfmanjm</title>
      <description>That was almost 2 years ago :) I'll have to do some research to see how I did it.. I'll get back to you.</description>
      <pubDate>Sun, 06 Sep 2009 21:33:06 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/33#comment-269</link>
      <guid isPermaLink='false'>urn:uuid:be8ba0ae-9276-4e8a-b8f2-0a30ce42cfb4</guid>
    </item>
  </channel>
</rss>
