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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:21:20+00:00 2026-06-18T02:21:20+00:00

Quick summary. An Offering has and belongs to many features A Feature has and

  • 0

Quick summary.

An Offering has and belongs to many features
A Feature has and belongs to many offerings

I want to find all Offerings that hace ALL the selected features.

Example:

  • The Offering_1 has the feature1
  • The Offering_2 has the feature2
  • The Offering_3 has the feature1 and the feature 2
  • The Offering_4 has the feature1, the feature 2 and the feature 3

When I call Offering.with_features([1,2,3]) I would expect to find ONLY the Offering_4 because is the only one that has the three features.

It might seem a silly question, but I can’t find a good join that returns that result. All joins I’ve tested returs the offerings that have ANY of the given features, not ALL.

Ideas?

UPDATE

Following doublea‘s advice, I created tables with their own ID instead of join tables.
It’s solution is still valid, and I’ve implemented like this:

# offering.rb
def self.with_features(features)
  if features && features.any?
    where(id: FeatureOfferingRelation.with_all_features(features).pluck(:offering_id))
  else
    scoped
  end
end

# feature_offering_relation.rb
def self.with_all_features(features)
  select(:offering_id)
    .where(feature_id: features)
    .group(:offering_id)
    .having("count(distinct feature_id) = ?", features.size)
end

It works!! But I’ll accept other ideas using joins instead of subqueries.

  • 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-18T02:21:21+00:00Added an answer on June 18, 2026 at 2:21 am

    Think the query will be something like this. Let me know if it works, else will test it out / fix it later.

    "select offering_id from features_offerings where feature_id in (?) 
    group by offering_id having count(distinct feature_id) = ?", 
    feature_ids, feature_count
    

    A couple things:

    1. Would recommend using a proper model as a join table, and thus has_many, :through, rather than has_and_belongs_to_many. Generally works out better for me. Think would make generating the query easier in this case.
    2. Would recommend tooling around with the Stanford DB class. The early videos on relational algebra and SQL queries were very helpful for me.

    http://class2go.stanford.edu/db/Winter2013

    Got to run. Again, reach out if you need more.

    Cheers,

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

Sidebar

Related Questions

Quick summary with what I now know I've got an EventWaitHandle that I created
Quick summary: I'm making an application that parses a binary file, stores vertices and
Quick summary: I have a Rails app that is a personal checklist / to-do
I'm baffled that I can't find a quick answer to this. I'm essentially looking
Quick Summary We have a Flash application (AS3) that gives users the ability to
Situation: I have many gzipped input files that I want to process in a
Quick summary of my problem: first rendering of the view contains the elements in
Quick question, indexof() find the first occur position of the string character? what about
Quick question, has anyone done a benchmark on random number generation between javascript and
Quick summary: in x86-64 mode, are far jumps as slow as in x86-32 mode?

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.