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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:19:52+00:00 2026-06-05T23:19:52+00:00

In a rails 3 app I’m building I have these three classes: class Instrument

  • 0

In a rails 3 app I’m building I have these three classes:

class Instrument < ActiveRecord::Base
  has_many :parts
  has_many :pieces, through: :parts
end    
class Part < ActiveRecord::Base
  belongs_to :instrument
  belongs_to :piece
end
class Piece < ActiveRecord::Base
  has_many :parts
  has_many :instruments, through: :parts
end

I am looking for a way to select all Pieces of which the associated instruments are a subset of an arbitrary set of instruments.

To avoid confusion I’ll give an example: given

  • Piece(id:1) with instruments: [1,2,3],
  • Piece(id:2) with [1,2,3,4]
  • Piece(id:3) with [1,3,4]
  • Piece(id:4) with [2],

somequery(1,2,3,4) should yield all four pieces, somequery(1,2,3) only 1 and 4, somequery(1,3,4) only piece 3 and somequery(2) only 4.

I’m using rails 3.2 and a solution in rails would be great but sql is also fine, and preferably postgres, but if there is a mysql-specific solution to this that would also be fine. Furthermore I’m not working with a relatively small database (1000+ pieces, 50 instruments and 15.000 parts) which isn’t heavily queried, so if the query is not optimal in efficiency that is no problem.

As a final disclaimer, I know my way around in ruby/rails, but am fairly green in SQL.

Thanks for the help!

  • 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-05T23:19:54+00:00Added an answer on June 5, 2026 at 11:19 pm
    DROP SCHEMA tmp CASCADE;
    CREATE SCHEMA tmp;
    
    SET search_path='tmp';
    
    
    CREATE TABLE instrument
            ( id INTEGER NOT NULL PRIMARY KEY
            , zname varchar
            );
    INSERT INTO instrument(id, zname) VALUES
    (1, 'instrument_1'), (2, 'instrument_2')
    , (3, 'instrument_3'), (4, 'instrument_4');
    
    CREATE TABLE piece
            ( id INTEGER NOT NULL PRIMARY KEY
            , zname varchar
            );
    INSERT INTO piece(id, zname) VALUES
    (1, 'piece_1'), (2, 'piece_2'), (3, 'piece_3'), (4, 'piece_4');
    
    CREATE TABLE has_part
            ( piece_id INTEGER NOT NULL
            , instrument_id INTEGER NOT NULL
            , PRIMARY KEY (piece_id,instrument_id)
            );
    
    INSERT INTO has_part(piece_id,instrument_id) VALUES
    (1,1), (1,2), (1,3)
    , (2,1), (2,2), (2,3), (2,4)
    , (3,1), (3,3), (3,4)
    , (4,2)
            ;
    

    The pure sql (not the double negation NOT EXISTS , NOT IN():

    SELECT zname
    FROM piece pp
    WHERE NOT EXISTS (
            SELECT * FROM has_part nx
            WHERE nx.piece_id = pp.id
            AND nx.instrument_id NOT IN (1,2,3)
            )
            ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A rails app I'm working on features examples of quadratic equations. Obviously, these are
I have a rails app that has asynchronous processing, and I'm having trouble getting
I have a Rails app that needs to expose values from a database as
I have a Rails app that archives high-res images (and thumbs) on S3 (via
We have a Rails app that instantiates a SWF object 16 times (it has
I have a rails app that I'm moving to another server and I figure
So I have a rails app with a url field for embedding youtube and
I am creating a rails app. And i have login in such a way
I have a rails app where i have a Contact model and with an
I am building a rails app that has a number of models, one of

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.