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:
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.
Hey thanks a ton!!
This worked for me aswell.
This comment has been removed by the author.
This is working in my application
Thanks
Nidhika
This comment has been removed by the author.
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
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