How to declare tag aliases with act_as_taggable_on in Rails?

The act_as_taggable_on implementation worked quite well, but I also need to declare the tag aliases.

I found a plugin that claimed to do this, acts_as_taggable_with_aliases , but the last commit was in 2009 and is not on gem repositories, so I assume the project is already dead.

Is there any way to achieve this?

+7
source share
2 answers

Perhaps you can create your own models to support this (and all you want) ...

I think you can achieve this by doing something like:

class Tag < ActiveRecord::Base end class Tagging < ActiveRecord::Base validates_presence_of :tag_id belongs_to :tag belongs_to :taggable, :polymorphic => true end class ModelIWantToBeTagged < ActiveRecord::Base include ModelTagging has_many :taggings, :as => :taggable end module ModelTagging def add_tag(tag_name) tag = Tag.find_or_create_by_tag(tag_name) tagging = Tagging.new tagging.taggable_id = self.id tagging.taggable_type = get_class_name tagging.tag_id = tag.id tagging.save! end def remove_tag(tag_name) tag = Tag.find_by_tag(tag_name) Tagging.where(:tag_id => tag).delete_all end private def get_class_name self.class.name end end 

This way you can use any behavior and data for your tags.

Hope this helps you!

+1
source

You can see the code acts_as_taggable_with_aliases . Everything is inside. You can see if it is compatible with acts_as_taggable and check if you can save it.

0
source

All Articles