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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:07:24+00:00 2026-05-14T15:07:24+00:00

I’m using rails 3.0 and MySql 5.1 I have these three models: Question, Tag

  • 0

I’m using rails 3.0 and MySql 5.1

I have these three models:

Question, Tag and QuestionTag.

Tag has a column called name.

Question has many Tags through QuestionTags and vice versa.

Suppose I have n tag names. How do I find only the questions that have all n tags, identified by tag name.

And how do I do it in a single query.

(If you can convince me that doing it in more than one query is optimal, I’ll be open to that)

A pure rails 3 solution would be preferred, but I am not adverse to a pure SQL solution either.

Please notice that the difficulty is in making a query which does not give all the questions that have any of the tags, but only the questions that have all the tags.

  • 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-14T15:07:24+00:00Added an answer on May 14, 2026 at 3:07 pm

    This is the solution I found for myself. Unmodified, it will only work in Rails 3 (or higher).

    In the Tag model:

    scope :find_by_names, lambda { |names| 
      unless names.empty?
        where("tags.name IN (#{Array.new(names.length, "?").join(",")})", *names) 
      else
        where("false")
      end
    }  
    

    In the Question model:

    scope :tagged_with, lambda { |tag_names| 
      unless tag_names.blank?
        joins(:question_tags).
        where("questions.id = question_tags.question_id").
        joins(:tags).where("tags.id = question_tags.tag_id").
        group("questions.id").
        having("count(questions.id) = ?", tag_names.count) & Tag.find_by_names(tag_names) 
      else
        scoped
      end
    }
    

    The & Tag.find_by_names(tag_names) combines the two scopes such that the join on tags is really a join on the scoped model.

    [Update]

    My sql-fu has improved a little, so I thought I’d offer a pure SQL solution also:

    SELECT q.*
    FROM (
    SELECT DISTINCT q.*
    FROM `questions` q
    JOIN question_tags qt
    ON qt.question_id = q.id
    JOIN tags t
    ON t.id = qt.tag_id
    WHERE t.name = 'dogs'
    ) AS q
    JOIN question_tags qt
    ON qt.question_id = q.id
    JOIN tags t
    ON t.id = qt.tag_id
    WHERE t.name = 'cats'
    

    This finds all the questions that have been tagged with both ‘cats’ and ‘dogs’. The idea is to have a nested subquery for each tag I want to filter by.

    There are several other ways to this. I’m not sure if it makes a difference to have the subquery in the FROM clause instead of the WHERE clause. Any insight would be appreciated.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and

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.