Using acts-as-taggable-on how do I find the top, say 10, tags in my app?
Asked Answered
B

1

3

Basically, I want to sort all the tags by the number of taggings they have.

I am trying to create navigation using the tags. So I want to display just the top 5, 10, 15, X tags sorted by the number of items they have tagged.

So I can't do any operation on a model, and there isn't a controller I can do it in either - I may just have to do it in the navigation view partial.

But I don't even know how to query acts-as-taggable-on to allow me to find the top X tags in the system.

How do I do that with acts-as-taggable-on?

I tried Tag model, but that doesn't seem to work.

Edit 1

When I call Item.tag_counts, this is what I see:

> Item.tag_counts
   (17.4ms)  SELECT items.id FROM "items" 
  ActsAsTaggableOn::Tag Load (17.1ms)  SELECT tags.*, taggings.tags_count AS count FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN items ON items.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Item' AND taggings.context = 'tags') AND (taggings.taggable_id IN(13)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id
 => [#<ActsAsTaggableOn::Tag id: 2, name: "paper">, #<ActsAsTaggableOn::Tag id: 1, name: "notepad">] 

Which is the 2 tags in the system. Is it ordered? How can I tell how many items were tagged by each?

Bomke answered 8/5, 2013 at 22:18 Comment(0)
C
4

You can use the tag_counts on the model class to retrieve a list of tags and their individual count.

Example with User model with default "skill" scope (courtesy of the documentation).

User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]

You may also use tag_counts_on(scope, options) which is basically the same.

Also, there is a RailsCast on this topic, explaning this gem:

EDIT after question edition: The list you have with Item.tag_counts is a list of tags having a count attribute. I am not sure they are already sorted but you can sort them by this attribute like this:

tags = Item.tag_counts.sort_by {|tag| tag.count}

EDIT for Final Answer Purposes

Or the Rails 3 version of the above query:

tags = Item.tag_counts.order(:count)
Carnation answered 8/5, 2013 at 22:40 Comment(4)
Yeh, I get that. But I want overall tag_counts. Not on 1 User. I am trying to build navigation and I want to put the tags in my navigation. I don't want to put every tag, I just want to put say the top 5, 10, 15 tags.Bomke
Are you sure you call tag_counts on the model class or on a model instance ? According to the quoted documentation, calling it on the class should do the trick.Carnation
I tried it myself on rails command, the returned tags have a overall count attribute. Updated my answer.Carnation
Or even more elegant and more Rails3-y,: Item.tag_counts.order(:count). I have confirmed that your suggestion works. Thanks. Just what the doctor ordered!Bomke

© 2022 - 2024 — McMap. All rights reserved.