Tag clouds with acts_as_taggable for different Taggable Types

So you are building a cool new web 2.0 startup and want to have the cool tagging feature. If you are using ruby on rails, you can use Acts_As_Taggable Plugin . There is a neat way to build a tag cloud explained here.

Now acts_as_taggable uses a field called taggable_type to know what object this tag is applied to. For example, you can have tags for videos, audio blah blah blah. Now you want to build a different tag cloud for each of these categories. The steps mentioned on the site do not support this. However a small modification in the SQL query used in file lib/tag.rb in the function self.tags(option={}) can achieve this. The original query is like this


def self.tags(options = {})
query = "select tags.id, name, count(*) as count"
query << " from taggings, tags"
query << " where tags.id = tag_id"
query << " group by tag_id"
query << " order by #{options[:order]}" if options[:order] != nil
query << " limit #{options[:limit]}" if options[:limit] != nil
tags = Tag.find_by_sql(query)
end

Change this to


query = "select tags.id, name, count(*) as count"
query << " from taggings, tags"
query << " where tags.id = tag_id and taggable_type = "+"'"+options[:type]+"' "
query << " group by tag_id"
query << " order by #{options[:order]}" if options[:order] != nil
query << " limit #{options[:limit]}" if options[:limit] != nil
tags = Tag.find_by_sql(query)

This adds the select based on taggable_type to the query and you can pass it as an option in the options hash. Now to build a tag cloud for audio model instead of the function call

@tags = Tag.tags(:limit => 100, :order => "name desc")

write

@tags = Tag.tags(:limit => 100, :order => "name desc",:type='Audio')

This will pull out tags only for audio and you can use this to build a tags cloud as exaplained in the post above

This is a small but useful modification. Do let me know if it can be further improved :)

Popularity: 16% [?]

find this blog feedworthy. Click on the number to subscribe!

Related Posts

No related posts

Comments

hi… i’m just starting out on ruby and rails.. and is trying to do my own tags-based information system.

would it be possible to see how the tables structure would look like for a typical tagging system ( for example, receipes has 1-to-many categories, categories have 1-to-many recipes)?

From what I understand, you will have several categories and then each category can have several recipes. You will be basically tagging recipes (and most likely not categories). So in your recipes model, add this line

class Recipe << ActiveRecord::Base

acts_as_taggable

….

This is assuming you have acts_as_taggable in your vendor/plugin directory. Now you can start tagging recipes. The Tags table would have taggable_type as ‘Recipe’. If you start tagging something later, that would be added to the table with taggable_type as ‘Whatever_model’

Hope this helps … check out acts_as_taggable wiki .. its pretty helpful

I am working on a local events site. I’d like to do a tag cloud for most popular event tags, but only for events that are current (so I won’t count old ones anymore) and approved for posting. I have an Event method that determines whether in event is current & approved for publication, publish? (true means it should be published). Any suggestions for how to modify this so that only tags for events where publish = true are counted?

[…] As Taggable On Steroids。 關於tag cloud Acts As Taggable Tag Cloud Tag clouds with acts_as_taggable for different Taggable Types with pagginate […]

Leave a comment

(required)

(required)