DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

How Snippets Gets General Post Counts For Tags

04.09.2005
| 6641 views |
  • submit to reddit
        This is in models/tag.rb

def find_all_with_count
    self.find_by_sql("SELECT t.*, COUNT(pt.post_id) AS count FROM tags t, posts_tags pt WHERE pt.tag_id = t.id GROUP BY t.id ORDER BY count DESC;")
end
This is a basic non-specific overview of all tags, as you would see on Snippets' front page. So:

@tag_info = Tag.find_all_with_count
@tag_info.each { |t| .. you now have t.name, t.count, etc .. }