ruby-on-railsacts-as-taggable-on

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


I have a Classs model with businesses, categories, and stages tag types.

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

And a ClassSearch form where users can input their business, category, and stage respectively, searching for Classses.

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)

I'm wondering if it's possible to rank/order @classses based on the number of tags that are similar to @class_search. If I'm not mistaken, acts_as_taggable_on ranks the results based on ID, which isn't too helpful in this case.

Thanks in advance :D


Solution

  • It's one of the available options that you can pass to the tagged_with method.

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

    Should give you a list ordered (descending) by number of matching tags.