# Acts\_as\_taggable and paginate?

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999>\
**Category:** Rails\
**Created:** [June 21, 2006, 4:57am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999 "2006-06-21T04:57:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 4:57am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/1 "2006-06-21T04:57:59Z")

</div>

Hi there,

I’ve been trying to paginate over a list of members that all share a tag  
in  
common using the acts\_as\_taggable plugin. The regular way of paginating  
over  
a collection doesn’t seem to work with acts\_as\_taggable. Here’s what my  
method looks like that takes in a tag name, finds all the members that  
share  
the tag and then displays all the members. Nothing too fancy at the  
moment…

def show\_all\_tags  
tag\_name = params[:id]  
@tagged\_members = Tag.find\_by\_name(tag\_name).tagged  
end

Doing the standard: @tagged\_member\_pages, @tagged\_members = paginate  
:member, :per\_page =\> 12 displays ALL the members in the DB.

Has anyone conquered such an issue like this?

Thank you,  
Dave H.

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 5:19am UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/2 "2006-06-21T05:19:16Z")

</div>

On Tuesday, June 20, 2006, at 9:55 PM, Dave H. wrote:

> Thank you,  
> Dave H.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

You want to do something like this…

def list  
if params[:tag] then  
@tags = params[:tag].split.map {|o| CGI::unescape(o)}  
@items = Item.find\_tagged\_with(@tags)  
@item\_pages = Paginator.new(ItemController, @items.size, 10)  
else  
@item\_pages, @items = paginate(:item, :order=\>:name)  
end  
end

just make sure your routes are set up for the :tag option…

map.connect ‘:controller/:action/:tag’, :action =\> ‘index’,:tag=\>nil,

\_Kevin

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/3 "2006-06-21T17:32:41Z")

</div>

Hi Kevin,

Thank you for the tips. I tried out what you suggested and it works as  
expected, accpet for one glitch in it. Here’s what my code looks like  
now:

def show\_all\_tags  
if params[:id] then  
@tags = params[:id].split.map {|o| CGI::unescape(o)}  
@members = Member.find\_tagged\_with(@tags)  
@member\_pages = Paginator.new(Member, @members.size, 2)  
else  
@member\_pages, @members = paginate(:member)  
end  
end

…and in the view:

 \<%= pagination\_links(@member\_pages) %\>

\<% for member in @members %\> ....do stuff.... \<% end %\>

I’m still getting all the members showing up, even if I limit the view  
to 2  
members per page. The paginate links show up correctly depending on how  
many  
members I designate to show, but the collection of members doesn’t obey  
the  
pagination. I have implemented pagination to work correctly in other  
areas  
of my app, but using the acts\_as\_taggable plugin (and methods) may very  
well  
be the culprit here. I really have no idea!

Any other suggestions?

Thanks!  
-Dave

On 21 Jun 2006 03:17:27 -0000, Kevin O. \<

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/4 "2006-06-21T20:40:14Z")

</div>

Hi Kevin,

I put the raise ‘test’ in before the paginate function, and when I run  
it I  
do see “test” in my browser. I also put a redirect to a general list  
action  
if no params come through. The “else” part of the block does work  
correctly.

def show\_all\_tags  
if params[:id] then  
@tag\_title = params[:id]  
@tags = params[:id].split.map {|o| CGI::unescape(o)}  
@members = Member.find\_tagged\_with(@tags)  
raise ‘test’  
@members\_pages = Paginator.new(Member, @members.size, 2)  
else  
redirect\_to :action =\> ‘list’  
end  
end

My route for this action looks like this:

map.connect ‘tags/:id’, :controller =\> ‘member’, :action =\>  
‘show\_all\_tags’

This URL does display members with a common tag:  
[http://localhost:3000/tags/photographer](http://localhost:3000/tags/photographer)

This URL does redirect back to a “list” action if no tag is present  
[http://localhost:3000/tags/](http://localhost:3000/tags/)

-Dave

On 21 Jun 2006 17:24:07 -0000, Kevin O. \<

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/5 "2006-06-21T21:55:16Z")

</div>

I found the following bit of code here (on the bottom of the page):  
[http://wiki.rubyonrails.org/rails/pages/ActsAsTaggablePluginHowto/versions/59](http://wiki.rubyonrails.org/rails/pages/ActsAsTaggablePluginHowto/versions/59)

module ActionController  
module Pagination  
MY\_OPTIONS = {:tag =\> ‘All’}  
DEFAULT\_OPTIONS.merge!(MY\_OPTIONS) {|key, old, new| old}

```
 alias old_find_collection_for_pagination 

```

find\_collection\_for\_pagination

```
 def find_collection_for_pagination(model, options, paginator)
   models = old_find_collection_for_pagination(model, options,

```

paginator)  
if options[:tag]==“All”  
models  
else  
models.delete\_if {|m| !m.is\_tagged\_with?(options[:tag])}  
end  
end  
end  
end

…but really have no idea WHERE to put it in the acts\_as\_taggable.rb  
file.  
How would you actually use something like this? Do you think this is a  
viable option?

Here’s the description of it:  
_Note_ _Acts\_as\_taggeble with pagination_  
The following opens the pagination class back up adds the abilty to  
filter  
based on a single tag. Pass a :tag =\> “tag” and the pagination with be  
base  
on though s items that are tagged with it. Belongs in  
acts\_as\_taggable.rb.  
EO

-Dave

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/6 "2006-06-21T19:24:41Z")

</div>

On Wednesday, June 21, 2006, at 10:28 AM, Dave H. wrote:

> else  
> \<% for member in @members %\>  
> be the culprit here. I really have no idea!
> 
> > > that share
> > 
> > end  
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

It looks like your action is falling through to the else clause.  
throw line like this in these before the paginate function

raise ‘test’

If it generates an error when you hit the action, then it’s not pulling  
the id out properly.

What does the url you are accessing the action with look like?  
What do your routes look like?

\_Kevin

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/7 "2006-06-21T22:38:45Z")

</div>

Hi Kevin (again),

I tried the MemberController change and that still didn’t work.

However… good news… I did solve it with the following code:

def show\_all\_tags  
per\_page = 12  
if params[:id] then  
@tag\_title = params[:id]  
@tags = params[:id].split.map {|o| CGI::unescape(o)}  
@members = Member.find\_tagged\_with(@tags)  
@members\_pages, @members = paginate(Member, {:conditions =\> [’  
tags.name = ?’, @tag\_title], :include =\> [:tags], :per\_page =\>  
per\_page})  
else  
redirect\_to :action =\> ‘list’  
end  
end

Thank you for all your help!  
-Dave H.

On 21 Jun 2006 20:15:10 -0000, Kevin O. \<

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [June 21, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-and-paginate/63999/8 "2006-06-21T22:18:08Z")

</div>

On Wednesday, June 21, 2006, at 1:38 PM, Dave H. wrote:

> ```
> @tag_title = params[:id]
> 
> ```
> 
> My route for this action looks like this:
> 
> > now:
> > 
> > > I’m still getting all the members showing up, even if I limit the  
> > > be the culprit here. I really have no idea!
> > > 
> > > >
> > 
> > my
> > 
> > > > > Doing the standard: @tagged\_member\_pages, @tagged\_members =  
> > > > > Rails mailing list  
> > > > > else  
> > > > > –  
> > > > > removed\_email\_address@domain.invalid
> > 
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I think your line like this…

```
 @member_pages = Paginator.new(Member, @members.size, 2)

```

should actually be

```
 @member_pages = Paginator.new(MemberController, @members.size, 2)

```

\_Kevin
