title: ActiveRecord finding record by relations requiring all attributes
body: <p>Ok, so I am new to ruby and rails and it will probably show with my question.</p>
  
  <p>I am writing a for fun app and have a particularly strange question in regards to finding records in a HABTM relationship. The underlying database is Postgresql</p>
  
  <p>My models look like the following</p>
  
  <pre><code>class Family &lt; ActiveRecord::Base
    has_and_belongs_to_many :persons
  end
  
  class Person &lt; ActiveRecord::Base
    has_and_belongs_to_many :families
  end
  </code></pre>
  
  <p>Lets say I have 4 people 'Joe', 'Jane', 'Mary', 'Ben'. They can all belong to multiple families.</p>
  
  <pre><code>Family 1 &lt;&lt; 'Joe', 'Jane', 'Ben'
  
  Family 2 &lt;&lt; 'Jane', 'Ben'
  
  Family 3 &lt;&lt; 'Joe', 'Jane', 'Ben', 'Mary'
  </code></pre>
  
  <p>I would like to be able to find families by searching for their names</p>
  
  <p>This is what my query currently looks like</p>
  
  <pre><code>Family.joins(:persons).where(persons: {name:['Joe','Jane','Ben']})
  </code></pre>
  
  <p>This works great at finding all records that either have Joe or Jane or Ben (all the families) but not at returning records that only have Joe, Jane, and Ben.</p>
  
  <p>In my example, I am looking to find only Family 1, not the other 2.</p>
  
  <p>How can I make sure that I am only finding records that have all of the names, no more, no less. </p>
  
  <p>Is there a better query to write or should I rethink my database structure?</p>