如何在Rails中使用acts_as_taggable_on声明标签别名?

时间:2020-12-22 11:07:16

The acts_as_taggable_on implementation worked quite well, but I also need to declare tags aliases.

acts_as_taggable_on实现工作得很好,但我还需要声明标签别名。

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

我发现了一个声称这样做的插件,acts_as_taggable_with_aliases,但是最后一次提交是在2009年并且不在gem存储库中,所以我认为该项目现在已经死了。

There is any way to achieve this?

有什么办法可以实现这个目标吗?

2 个解决方案

#1


1  

Maybe you can create your own models to support this (and anything else you want)...

也许你可以创建自己的模型来支持这个(以及你想要的任何其他东西)......

I think you can achieve that 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 any behavior and data to your tags.

这样,您可以将任何行为和数据添加到标记中。

Hope it helps you!

希望它能帮到你!

#2


0  

You can take a look to the code of acts_as_taggable_with_aliases. All is inside. You can see if it's allways compatible with acts_as_taggable and check if you can try maintain it.

您可以查看acts_as_taggable_with_aliases的代码。一切都在里面。你可以看看它是否总是与acts_as_taggable兼容,并检查你是否可以尝试维护它。

#1


1  

Maybe you can create your own models to support this (and anything else you want)...

也许你可以创建自己的模型来支持这个(以及你想要的任何其他东西)......

I think you can achieve that 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 any behavior and data to your tags.

这样,您可以将任何行为和数据添加到标记中。

Hope it helps you!

希望它能帮到你!

#2


0  

You can take a look to the code of acts_as_taggable_with_aliases. All is inside. You can see if it's allways compatible with acts_as_taggable and check if you can try maintain it.

您可以查看acts_as_taggable_with_aliases的代码。一切都在里面。你可以看看它是否总是与acts_as_taggable兼容,并检查你是否可以尝试维护它。