Showing only the most popular tags in the acts_as_taggable_on tag cloud

◇◆丶佛笑我妖孽 提交于 2019-12-04 07:13:15

You can use MyModel.tag_counts_on(:tags) to get a list of tags, ordered by tag count descending (most used tag first) and if you want to limit that to a specific number, you can just append .limit(my_magic_number) to it.

So to get a list of the 10 most popular tags on your Post model you'd do something like this:

@tag_counts = Post.tag_counts_on(:tags).limit(10)

If you then want to see how many times each tag has been used, the objects in @tags each have a count attribute you can look at.

EDIT: (extracted from one of my comments below) ...and if you want to the tags in a specific order (the most used tags first) with some externally defined limit, you can use this: Post.tag_counts_on(:tags).order('count desc').limit(however_many_you_want)

According to the documentation, you just have to pass the options you need for your calculations, in your case (the controller):

  def styles_tag_cloud
     @tags = Tattoo.tag_counts_on(:styles).limit(40)
  end

Your view remains the same. Regards!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!