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 9201599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:03:13+00:00 2026-06-17T23:03:13+00:00

For the purpose of this question, let’s assume I’m building Stack Overflow. I’ve got

  • 0

For the purpose of this question, let’s assume I’m building Stack Overflow.

I’ve got a many-to-many relationship via an association table for Posts and Tags, and I’d like to search for posts that contain multiple tags.

I can search for posts that contain a single tag like this:

scope :has_tag, lambda { |tag| includes(:tags).where("lower(tags.name) = ?", tag.downcase) }

Post.has_tag("Python")

For searching for multiple tags, I’ve tried building up a query using a loop:

def self.has_tags(tags)
  query = includes(:tags)
  tags.each do |tag|
    query = query.where("lower(tags.name) = ?", tag)
  end
  return query
end

This results in the query ... WHERE (lower(tags.name) = 'tag1') AND (lower(tags.name) = 'tag2'), which doesn’t match anything.

I’ve also tried using .where('tags.name IN (?)', tags), but I think that matches any post that contains one of the tags instead of posts that contain all the tags.

Now I’m thinking that perhaps I should use the ID of the tags, but that sounds like it could result in multiple queries. Is this inefficient?

... WHERE tag_id IN (SELECT id FROM tag WHERE name IN ('tag1','tag2'))

What’s the best way to search for multiple tags?
(I’d prefer to understand the syntax and do it manually rather than using a gem, since I’m new to Rails.)

  • 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-06-17T23:03:14+00:00Added an answer on June 17, 2026 at 11:03 pm

    I ended up using the following:

    scope :has_tags, lambda { |tags| includes(:tags)
      .where("lower(tags.name) IN (?)", tags.collect { |tag| tag.downcase } )
      .group('"posts"."id"')
      .having('COUNT(DISTINCT "tags"."id") = ?', tags.count) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a game for Android. For the purpose of this question let's assume
(For the purposes of this question, let us assume that one is intentionally not
Ok this may be a silly question for many of you. Let me preface
For the purpose of this questions, let's assume I have these two Entities defined
Let's just say for the purpose of this question, we have a blog. Now,
Based on this question I understand the purpose of the construct in linking C
I posted this question here on purpose, although - for my case - it
Quick question, What have I done wrong here. The purpose of this code is
My question is this: Doesn't storing the salt as plaintext defeat the purpose of
I want to premise that this question's purpose is checking if there's at least

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.