Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:14:56+00:00 2026-05-30T14:14:56+00:00

I have a Forum and ForumTag HABTM relationship. I also have an array of

  • 0

I have a Forum and ForumTag HABTM relationship. I also have an array of variables named @tags . This array contains the names of some ForumTags. I want to be able to query and find all forums that have ALL the values of the array. I currently have:

@forums = Forum.joines(:forum_tags).where(:forum_tags => {:name => @tags}).includes(:forum_tags).all

However, this returns all the forums that have AT LEAST ONE value in the array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:14:57+00:00Added an answer on May 30, 2026 at 2:14 pm

    The following will require the forums to have all the forum tags in the @tags array. I am making the assumption that a forum will not have the same forum_tag more than once.

    @forums = Forum.joins(:forum_tags).where(:forum_tags => {:name => @tags}).group("forums.id").having(['COUNT(*) = ?', @tags.length]).includes(:forum_tags).all
    

    This will produce an SQL query like the following:

    @tags = ['foo', 'bar']
    
    SELECT forums.id, forum_tags.id FROM forums
      LEFT OUTER JOIN forum_tags_forums on forum_tags_forums.forum_id = forums.id
      LEFT OUTER JOIN forum_tags ON forum_tags.id = forum_tags_forums.forum_tag_id
      WHERE forum_tags.name IN ('foo', 'bar')
      GROUP BY forums.id
      HAVING COUNT(*) = 2;
    

    This will group all the rows in the join table by forums that match the given tags. If the COUNT function has the value of the total number of tags that you’re looking for (and there are no duplicate forum/forum_tag pairs) then the forum must contain all the tags.

    To get the leftover tags (question asked in the comments):

    forum_tags = ForumTag.where(:name => @tags)
    
    @forums_with_leftovers = Forum.select("forums.*, GROUP_CONCAT(forum_tags.name) AS leftover_tags").joins(:forum_tags).where(['forums.id IN (?) AND NOT forum_tags.id IN (?)', @forums, forum_tags]).group("forums.id").all
    

    Each Forum object in @forums_with_leftovers will have an extra attribute leftover_tags that contains a comma separated list of tags in each forum object that is not in the original @tags variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this forum for a while now to find answers to some
I have a discussion forum/blog engine working with web forms. Each post contains html
I have a idea of building a unique forum. This forum would focus on
I have a forum and visitors can register user names with more than 20
I have a forum, user's can post comments, they can also enable/disable and approve
You guys on this forum have been a great help so far, and i
I have searched the forum and have not found an issue similar to this.
i have a forum with 1..n members each having 1..n articles, so this is
I have seem many forum posts / responses on this subject but I am
I have tried some forum pages and searched through stackoverflow for the same thing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.