Here's what I'm using with Acts As Taggable (On Steroids).
I've added a method to the Tag model class for retrieving the N most popular tags
def self.find_most_popular(max_count = 0) count_col = Tag.connection.quote_column_name('count') sql = "SELECT tags.id AS id, tags.name AS name, COUNT(*) AS #{count_col}" sql << " FROM taggings JOIN tags ON taggings.tag_id = tags.id" sql << " GROUP BY tag_id ORDER BY #{count_col} DESC" sql << " LIMIT #{max_count.to_i}" if max_count.to_i > 0 tags = Tag.find_by_sql(sql) tag_count = tags.size tags.each do |tag| tag.send(:write_attribute, :percentage, (tag.count * 100) / tag_count) end tags end
Acts As Taggable On Steroids has some other functionality for this, see its REAME.
In the PopularTagsHelper I have a method like this
def popular_tag(t, options = {}) wrap = options[:wrap] || 'span' font_size = t.percentage * (options[:size_scale] || 12) font_weight = t.percentage * (options[:weight_scale] || 10) font_size = [font_size, options[:min_size]].max if options[:min_size] font_size = [font_size, options[:max_size]].min if options[:max_size] content_tag(wrap, link_to(t.name, ''), :title => "#{t.count} times", :class => options[:class], :style => "font-size:#{font_size}%;font-weight:#{font_weight}%" ) end
In the view
<ul class="tagcloud"> <% @popular_tags.sort_by(&:name).each do |t| -%> <%= popular_tag(t, :wrap => 'li', :min_size => 30, :max_size => 400) %> <% end -%> </ul>
Finally some CSS
.tagcloud { text-align: center; width: 70%; } .tagcloud li { display: inline-block; display: -moz-inline-box; white-space: nowrap; vertical-align: middle; line-height: 1.2em; padding: 0 0.2em; }
On a self-serving note, let me point you to http://schuerig.de/michael/blog/index.php/2007/02/14/popular-requests/
HTH, Michael