Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return all tags based on context - ActsAsTaggableOn

I'm using the rails gem acts-as-taggable on, and am tagging posts on two contexts: tags and topics.

To return a hash of all the topics tags used so far for posts I can use the code:

 Post.tag_counts_on(:topics)

However, I have created a certain number of set topics tags, and if some of these topics tags aren't currently being used as tags on posts, then the code above doesn't return the said topics.

I am wondering if there is a way to return all the relevant tags based on context -- I was hoping for a solution along the lines of :

 topics = Tag.topics

To implement the solution, I created a Tag.rb model:

 class Tag < ActiveRecord::Base
   has_many :relationship_topics, :foreign_key => "topic_followed_id", :dependent => :destroy
   has_many :topic_followers, :through => :relationship_topics, :source => :topic_follower
 end

Here I have some code to allow for following topics, but nothing more.

Does anyone know how I could return all the tags based on context?

like image 575
jay Avatar asked Aug 24 '11 02:08

jay


People also ask

How to use acts as taggable?

Rather than tying functionality to a specific keyword (namely tags ), acts as taggable on allows you to specify an arbitrary number of tag "contexts" that can be used locally or in combination in the same way steroids was used. To use it, add it to your Gemfile: Review the generated migrations then migrate :

What is actsastaggableon?

ActsAsTaggableOn. This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney. It has evolved substantially since that point, but all credit goes to him for the initial tagging functionality that so many people have used. For instance, in a social network, a user might have tags that are called skills, interests, sports,...

How do I manage the tags in the model context?

With the defined context in model, you have multiple new methods at disposal to manage and view the tags in the context. For example, with :skill context these methods are added to the model: skill_list (and skill_list.add, skill_list.remove skill_list= ), skills (plural), skill_counts.

How to implement acts as taggable on steroids?

For instance, in a social network, a user might have tags that are called skills, interests, sports, and more. There is no real way to differentiate between tags and so an implementation of this type is not possible with acts as taggable on steroids. Enter Acts as Taggable On.


1 Answers

I have never used acts-as-taggable-on but a quick browse through of the code suggests, you can do:

# to get all the tags with context topic with counts
ActsAsTaggableOn::Tagging.
    includes(:tag).
    where(:context => "topics").
    group("tags.name").
    select("tags.name, COUNT(*) as count")

You should probably take a look at ActsAsTaggableOn::Tagging, ActsAsTaggableOn::Tag and the migration file in your db/migrations folder to figure out how you can go about it.

If you don't want the count, only the tag names:

tags = ActsAsTaggableOn::Tag.includes(:taggings).
           where("taggings.context = 'topics'").
           select("DISTINCT tags.*")

# usage
tags.each {|tag| puts tag.name}

I hope that answers your question.

like image 138
rubish Avatar answered Oct 17 '22 19:10

rubish