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

  • Home
  • SEARCH
  • 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 6097401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:02:20+00:00 2026-05-23T13:02:20+00:00

OK, the title is not that good… Here is the example : class Product

  • 0

OK, the title is not that good…

Here is the example :

class Product
  has_and_belongs_to_many :categories
end

I want to create a scope that returns Products that have ALL the categories IDs I’ve sent as argument.

If I use Product.includes(:categories).where(:”categories.categorie_id” => [1,2,3,4]) it gives me all Products that have any of categories 1, 2, 3, 4.
I would like Products which have at least all the categories I send as parameter.

For example :

Product.with_all_categories([1, 2, 3, 4]) # => get all the Product that have categories 1, 2, 3 AND 4 (at least, it could be more).
  • 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-23T13:02:20+00:00Added an answer on May 23, 2026 at 1:02 pm

    I guess you won’t like the answer : this is not easy.

    AFAIK, it’s not something you can do in AR directly. You have to go through find_by_sql. The request you need is something like :

    SELECT products.* FROM products
    INNER JOIN categories_products cat1 ON products.id = 
    cat1.product_id
    INNER JOIN categories_products cat2 ON products.id = 
    cat2.product_id
    INNER JOIN categories_products cat3 ON products.id = 
    cat3.product_id
    WHERE (cat1.category_id = 1) and (cat3.category_id = 2) and (cat3.category_id = 3)
    

    adding an inner join and one and clause for every category.
    There is other queries possibles, this one should perform well in MySQL.

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

Sidebar

Related Questions

OK so that title sucks a little but I could not think of anything
I know, the title does not sound good however I will explain it clearly.
The title may not really explain what I'm really trying to get at, couldn't
May be my title is not clear. I am looking for some kind of
Not sure if the title is quite right for the question but I can't
I'm not sure if the title is very clear, but basically what I have
I'm not really sure how to title this question but basically I have an
I'm sorry I could not think of a better title. The problem is the
How can I make my window not have a title bar but appear in
Long winded title, short question: If one wants to develop for Windows but not

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.