根据上下文返回所有标签 - ActsAsTaggableOn

时间:2011-08-24 02:23:41

标签: ruby-on-rails ruby-on-rails-3 tags acts-as-taggable-on

我正在使用rails gem act-as-taggable,并在两个上下文中标记帖子:标签和主题。

要返回目前为帖子使用的所有主题标签的哈希,我可以使用代码:

 Post.tag_counts_on(:topics)

但是,我已创建了一定数量的设置主题标签,如果其中某些主题标签当前未用作帖子上的标签,则上述代码不会返回上述主题。

我想知道是否有办法根据上下文返回所有相关标签 - 我希望找到一个解决方案:

 topics = Tag.topics

为了实现该解决方案,我创建了一个Tag.rb模型:

 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

这里我有一些代码可以允许以下主题,但仅此而已。

有谁知道如何根据上下文返回所有标签?

4 个答案:

答案 0 :(得分:16)

我从未使用acts-as-taggable-on,但快速浏览代码表明,您可以这样做:

# 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")

您可能应该查看ActsAsTaggableOn::TaggingActsAsTaggableOn::Tag以及db / migrations文件夹中的迁移文件,以了解如何进行此操作。

如果您不想计算,只需标记名称:

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

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

我希望能回答你的问题。

答案 1 :(得分:0)

使用Model.tag_counts(来自Using acts-as-taggable-on how do I find the top, say 10, tags in my app?):

User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]

答案 2 :(得分:0)

这对我来说效果最好:

ActsAsTaggableOn::Tag.includes(:taggings).where(taggings:{context:'topics'}).uniq(:name).order(:name)

使用标记上下文执行joinsincludes时的一个限制是您只能看到处于活动状态的主题。您无法加载主题列表并使用此查询显示它们。见例子:

不标记上下文

2.2.1 :009 > ActsAsTaggableOn::Tag.includes(:taggings)
  ActsAsTaggableOn::Tag Load (0.4ms)  SELECT `tags`.* FROM `tags`
  ActsAsTaggableOn::Tagging Load (0.4ms)  SELECT `taggings`.* FROM `taggings` WHERE `taggings`.`tag_id` IN (1, 2, 3)
[
    [0] severe hearing loss {
                    :id => 1,
                  :name => "severe hearing loss",
        :taggings_count => 0
    },
    [1] hearing loss {
                    :id => 2,
                  :name => "hearing loss",
        :taggings_count => 1
    },
    [2] hearing aids {
                    :id => 3,
                  :name => "hearing aids",
        :taggings_count => 0
    }
]

标记内容为topics

2.2.1 :016 > ActsAsTaggableOn::Tag.includes(:taggings).where(taggings:{context:'topics'})
  SQL (0.4ms)  SELECT `tags`.`id` AS t0_r0, `tags`.`name` AS t0_r1, `tags`.`taggings_count` AS t0_r2, `taggings`.`id` AS t1_r0, `taggings`.`tag_id` AS t1_r1, `taggings`.`taggable_id` AS t1_r2, `taggings`.`taggable_type` AS t1_r3, `taggings`.`tagger_id` AS t1_r4, `taggings`.`tagger_type` AS t1_r5, `taggings`.`context` AS t1_r6, `taggings`.`created_at` AS t1_r7 FROM `tags` LEFT OUTER JOIN `taggings` ON `taggings`.`tag_id` = `tags`.`id` WHERE `taggings`.`context` = 'topics'
[
    [0] hearing loss {
                    :id => 2,
                  :name => "hearing loss",
        :taggings_count => 1
    }
]

答案 3 :(得分:-1)

方法很简单:

ActsAsTaggableOn::Tag.for_context('topics')