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

发布于 2024-12-01 03:00:50 字数 672 浏览 0 评论 0原文

我正在使用 Rails gem 充当可标记的角色,并在两个上下文中标记帖子:标签和主题。

要返回迄今为止用于帖子的所有主题标签的哈希值,我可以使用以下代码:

 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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

驱逐舰岛风号 2024-12-08 03:00:50

我从未使用过 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::标记, ActsAsTaggableOn::Tag 和 db/migrations 文件夹中的迁移文件,以了解如何进行操作。

如果您不需要计数,只需要标签名称:

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

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

我希望这能回答您的问题。

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.

情仇皆在手 2024-12-08 03:00:50

使用 Model.tag_counts (来自 使用acts-as-taggable-on如何在我的应用程序中找到顶部(例如10个)标签?):

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

Use Model.tag_counts (from 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>...]
苹果你个爱泡泡 2024-12-08 03:00:50

这对我来说效果最好:

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
    }
]

主题的标记上下文

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
    }
]

This worked best for me:

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

One limitation when doing joins or includes with a tagging context is that you will only see topics that are active. You cannot load up a topic list and have them show up using this query. See examples:

Without tagging context

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
    }
]

With taggings context of 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
    }
]
无所谓啦 2024-12-08 03:00:50

方法很简单:

ActsAsTaggableOn::Tag.for_context('topics')

The method is very simple:

ActsAsTaggableOn::Tag.for_context('topics')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文