I have 3 tables/models Participants, Demographics, ParticipantDemographics.
I would like to find Participants who have a combination of Demographics.
Here is a simple example:
Lets presume in some UI I am trying to filter a list of participants. I select the following demographic choices. Gender: Male (id=1), Age: 21-30 (id=7), Age: 31-40 (id=8)
This indicates I want Males within the age group 21-30 or 31-40
I would like find participants who have a participant_demographic record where the demographic_id is either 7 or 8, and they also have a participant_demographic record where the demographic_id is 1.
What does this SQL statement look like and is there anything in ActiveRecord that can make this select look nicer or be chained more easily.
Thank You
Edit: There seems to be some confusion as to why I would use a HABTM. In this system the end user can create any demographics they would like to track. Not just ones I've predefined and put on a table as columns. For example a user might want to start tracking the "spoken language" of the participants. So they would add the "spoken language" category and demographics for each "english", "spanish", "german", "etc". Because people can speak many languages I could link through the join table a participant to both english and spanish. This is why the join table is necessary.