Rails ActsAsTaggableOn,根据相似标签的数量对结果进行排名 "tagged_with"

Rails ActsAsTaggableOn, rank "tagged_with" result by number of similar tags

我有一个包含业务、类别和阶段标签类型的类模型。

classs.rb:

  acts_as_taggable_on :businesses
  acts_as_taggable_on :categories
  acts_as_taggable_on :stages

  BUSINESSES = [
    "Service Professional (Financial Advisor, Realtor, etc.)",
    "E-Commerce",
    "F&B",
    "Education & Coaching",
    "Start-ups",
    "B2B",
    "B2C",
    "Others"
  ]

  CATEGORIES = [
    "Social Media Marketing",
    "Lead Generation",
    "Content Strategy",
    "Marketing Strategy",
    "Branding Strategy"
  ]

  STAGES = [
    "Development (Little to no revenue)",
    "Early (Small customer base with some market presence)",
    "Growth (Established customer base and large amount of revenue)",
    "Mature (Large customer base and profits)"
  ]

还有一个ClassSearch表单,用户可以在其中分别输入他们的业务、类别和阶段,搜索Classes。

class_searches_controller#show

@filter = @class_search.categories.push(@class_search.business).push(@class_search.stage).flatten.reject(&:blank?)
@classses = Classs.all.tagged_with(@filter, any: true)

我想知道是否可以rank/order @classses 基于类似于@class_search 的标签数量。如果我没记错的话,acts_as_taggable_on 会根据 ID 对结果进行排名,这在这种情况下并没有太大帮助。

提前致谢:D

它是您可以传递给 tagged_with 方法的可用选项之一。

@classses = Classs.all.tagged_with(@filter, any: true, order_by_matching_tag_count: true)

应该给你一个按匹配标签数量排序(降序)的列表。