Gem acts_as_taggable_on 获取标签名字及每个标签的数量

zhaojiahui · 2013年05月10日 · 2582 次阅读

ruby: 2.0.0p0, rails: 3.2.13 , acts-as-taggable-on 2.4.1 正在使用acts_as_taggable_on来打标签,现在我获取到每个标签名字及对应的数量

2.0.0p0 :049 > Question.tag_counts
   (0.3ms)  SELECT questions.id FROM "questions" 
  ActsAsTaggableOn::Tag Load (0.3ms)  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 questions ON questions.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Question' AND taggings.context = 'tags') AND (taggings.taggable_id IN(10,17,18,22,23,24,26,27,28,29,30,31,32)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id

这个貌似只返回了 tag_id 和 tag name。求指导。

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号