Sunday, March 05, 2006

acts_as_taggable pagination

Recently I needed to paginate the results of find_tagged_with method. I am using the acts_as_taggable plugin.

I did not want to paginate the collection after it has been retrieved since the data set I am working with is huge.

Just for reference, using the paginate_collection function, you'll use something like this to paginate your collection:

q_favorites=Favorite.find_tagged_with(:any => params[:tag], :separator=>',', :order=>'updated_at DESC')
@favorite_pages, @favorites = paginate_collection(:collection=>q_favorites, :page => @params[:page])



So, after looking around (and not finding a solution), I finally decided to go the query way.

Here's my solution based on paginate example in the Wiki. Enjoy:


page = (params[:page] ||= 1).to_i
items_per_page = 3
offset = (page - 1) * items_per_page

@webpage_count = Webpage.count("tags_webpages.tag_id = tags.id AND ( tags.name = 'Accounting' ) AND webpages.id = tags_webpages.webpage_id",
joins = ", tags_webpages, tags")

@webpage_pages = Paginator.new(self, @webpage_count, items_per_page, page)


@webpages=Webpage.find_tagged_with(:any => params[:tag], :limit=>items_per_page, :offset=>offset, :separator=>',', :order=>'updated_at DESC')





If you can think of something better, please post a comment.

7 Comments:

At 1:32 AM, Blogger Unknown said...

using acts_as_taggable on steroids,
i got this working

@document_pages, @documents = paginate :documents,
{:conditions => ["title like ? and tags.name=?","%#{query_text}%",tag],
:joins => "INNER JOIN taggings ON documents.id = taggings.taggable_id INNER JOIN tags ON taggings.tag_id = tags.id",

:order=>"created_at DESC",
:select=>"documents.*"

}


The last select is important, otherwise rails don't pick the correct ID fields, and you get very interesting results! :)
I scratched my head on that one for a good while.

 
At 1:28 AM, Blogger Vivek said...

Hey thanks a ton!!

This worked for me aswell.

 
At 1:52 AM, Blogger Nidhika said...

This comment has been removed by the author.

 
At 1:55 AM, Blogger Nidhika said...

This is working in my application

Thanks
Nidhika

 
At 11:14 PM, Blogger Nidhika said...

This comment has been removed by the author.

 
At 11:16 PM, Blogger Nidhika said...

Can you solve my problem
In pagination I have two link "all" and "mine" when I am click on "all" it go into another link "mine" and again click on "all" it display associate page.
How I solve my problem.

Thanks
Nidhika

 
At 5:43 PM, Anonymous Anonymous said...

Do you know fiesta Gold? I like it.
My brother often go to the internet bar to buy fiesta money and play it.
After school, He likes playing games using these fiesta online gold with his friend.
I do not like to play it. Because I think that it not only costs much money but also spend much time. One day, he give me many buy fiesta Gold and play the game with me.
I came to the bar following him and found fiesta online money was so cheap. After that, I also go to play game with him.


Do you know fiesta Gold? I like it.
My brother often go to the internet bar to buy fiesta money and play it.
After school, He likes playing games using these fiesta online gold with his friend.
I do not like to play it. Because I think that it not only costs much money but also spend much time. One day, he give me many buy fiesta Gold and play the game with me.
I came to the bar following him and found fiesta online money was so cheap. After that, I also go to play game with him.

 

Post a Comment

<< Home

eXTReMe Tracker