# Solution: Getting will\_paginate and acts\_as\_taggable to play

**URL:** <https://www.ruby-forum.com/t/solution-getting-will-paginate-and-acts-as-taggable-to-play/104855>\
**Category:** Rails\
**Created:** [May 23, 2007, 8:25am UTC](https://www.ruby-forum.com/t/solution-getting-will-paginate-and-acts-as-taggable-to-play/104855 "2007-05-23T08:25:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 23, 2007, 8:25am UTC](https://www.ruby-forum.com/t/solution-getting-will-paginate-and-acts-as-taggable-to-play/104855/1 "2007-05-23T08:25:13Z")

</div>

I’m using both will\_paginate [http://errtheblog.com/post/4791](http://errtheblog.com/post/4791) and  
acts\_as\_taggable plugins on the app [http://www.themotherhood.net/](http://www.themotherhood.net/) I’m  
working on. The tagged results need to be paged just like other lists.  
Here’s my hack to get the two to work together:

RAILS\_ROOT/lib/paginated\_tags.rb in lib:

module ActiveRecord  
module Acts #:nodoc:  
module Taggable #:nodoc:  
module SingletonMethods

```
    def find_tagged_with(list,*args)
      #build the limit sql
      options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
      limit,offset = options.delete(:limit), options.delete(:offset)

```

unless options.empty?  
scope = (limit && offset) ? “LIMIT #{offset}, #{limit}” : “”

```
      find_by_sql([
        "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " 

```

- 

```
      "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " 

```

- 

```
      "AND taggings.taggable_type = ? " +
      "AND taggings.tag_id = tags.id AND tags.name IN (?) 

```

#{scope}",  
acts\_as\_taggable\_options[:taggable\_type], list  
])

```
    end
    #will_paginate will call find_all_tagged_with
    alias find_all_tagged_with find_tagged_with
  end
end

```

end  
end

I can now call a method like this:

@stories = Story.paginate\_tagged\_with(“goo”, :total\_entries =\> 2, :page  
=\>  
params[:page])

You have to manually pass in the total\_entries, but hacking the code  
where  
total\_entries is set in the will\_paginate finder method could get around  
this.

- Nathan Verni
