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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:33:36+00:00 2026-05-15T20:33:36+00:00

I have two models, Project and Category, which have a many-to-many relationship between them.

  • 0

I have two models, Project and Category, which have a many-to-many relationship between them. The Project model is very simple:

class Project < ActiveRecord::Base
  has_and_belongs_to_many :categories

  scope :in_categories, lambda { |categories|
    joins(:categories).
    where("categories.id in (?)", categories.collect(&:to_i))
  }
end

The :in_categories scope takes an array of Category IDs (as strings), so using this scope I can get back every project that belongs to at least one of the categories passed in.

But what I’m actually trying to do is filter (a better name would be :has_categories). I want to just get the projects that belong to all of the categories passed in. So if I pass in [“1”, “3”, “4”] I only want to get the projects that belong to all of the categories.

  • 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-15T20:33:36+00:00Added an answer on May 15, 2026 at 8:33 pm

    There are two common solutions in SQL to do what you’re describing.

    Self-join:

    SELECT ...
    FROM Projects p
    JOIN Categories c1 ON c1.project_id = p.id
    JOIN Categories c3 ON c3.project_id = p.id
    JOIN Categories c4 ON c4.project_id = p.id
    WHERE (c1.id, c3.id, c4.id) = (1, 3, 4);
    

    Note I’m using syntax to compare tuples. This is equivalent to:

    WHERE c1.id = 1 AND c3.id = 3 AND c4.id = 4;
    

    In general, the self-join solution has very good performance if you have a covering index. Probably Categories.(project_id,id) would be the right index, but analyze the SQL with EXPLAIN to be sure.

    The disadvantage of this method is that you need four joins if you’re searching for projects that match four different categories. Five joins for five categories, etc.

    Group-by:

    SELECT ...
    FROM Projects p
    JOIN Categories cc ON c.project_id = p.id
    WHERE c.id IN (1, 3, 4)
    GROUP BY p.id
    HAVING COUNT(*) = 3;
    

    If you’re using MySQL (I assume you are), most GROUP BY queries invoke a temp table and this kills performance.

    I’ll leave it as an exercise for you to adapt one of these SQL solutions to equivalent Rails ActiveRecord API.

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

Sidebar

Related Questions

Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have two models with managers: Category and Project. class PopulatedCategoriesManager(models.Manager): def get_query_set(self): return
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
Let's say I have two simple models project t.string :title vote t.references :project t.integer
I have two models: cars and pictures in my RoR project class Car <
The problem is I have two models - Project and Ticket (which belongs to
I'm investigating on how validates_presence_of actually works. Suppose I have two models class Project
I'm using MetaSearch gem in my Rails 3 project. I have two models: class
I have two models. A project and a backers model. I want to basically
I have a has_many and belongs_to association set up between two models: Project 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.