使用acts-as- taggabon,我如何找到顶部,比如10,标签在我的应用?

时间:2021-03-27 11:06:50

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.

我正在尝试使用标签创建导航。所以我想要显示前5 10 15 X标签按标签的数量排序。

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.

但我甚至不知道如何查询acos -as- taggabon,以允许我找到系统中的*X标记。

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

我该如何用动作-as- taggabon来做呢?

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

我试过Tag model,但似乎行不通。

Edit 1

编辑1

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

当我调用项目。tag_counts,这是我看到的:

> 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?

也就是系统中的两个标签。这是命令吗?我怎么知道每个标签上有多少项?

1 个解决方案

#1


4  

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

您可以使用模型类的tag_counts来检索标记的列表及其各自的计数。

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.

您还可以使用tag_counts_on(作用域、选项),这基本上是相同的。

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:

编辑后的问题版本:你的项目列表。tag_counts是具有count属性的标记列表。我不确定它们是否已经排序,但是您可以按照如下属性对它们进行排序:

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

EDIT for Final Answer Purposes

为最终答案而编辑

Or the Rails 3 version of the above query:

或上述查询的Rails 3版本:

tags = Item.tag_counts.order(:count)

#1


4  

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

您可以使用模型类的tag_counts来检索标记的列表及其各自的计数。

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.

您还可以使用tag_counts_on(作用域、选项),这基本上是相同的。

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:

编辑后的问题版本:你的项目列表。tag_counts是具有count属性的标记列表。我不确定它们是否已经排序,但是您可以按照如下属性对它们进行排序:

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

EDIT for Final Answer Purposes

为最终答案而编辑

Or the Rails 3 version of the above query:

或上述查询的Rails 3版本:

tags = Item.tag_counts.order(:count)