title: PostgreSQL query get all entries with many-to-many relationship with another table
body: <p>I have a table <code>posts</code> which has a many-to-many relationship with <code>tags</code>, the pivot table is called <code>posts-tags</code>.</p>
  <p>I want to be able to retrieve all <code>posts</code> by a list of tag id's.</p>
  <p>Imagine</p>
  <pre><code>posts
  
  id | text
  --------
  1  | &quot;foo&quot;
  2  | &quot;bar&quot;
  3  | &quot;baz&quot;
  </code></pre>
  <pre><code>posts_tags
  
  post_id | tag_id
  -----------------
  1       | 1
  1       | 2
  1       | 3
  2       | 1
  3       | 1
  </code></pre>
  <pre><code>tags
  
  id | name
  --------
  1  | &quot;foo&quot;
  2  | &quot;bar&quot;
  3  | &quot;baz&quot;
  
  </code></pre>
  <p>With tag id's <code>[1, 2, 3]</code>, I should get back <code>[{id: 1, text: &quot;foo&quot;}]</code></p>
  <p>With tag id's <code>[1]</code>, I should get back <code>[{id: 1, text: &quot;foo&quot;}, {id: 2, text: &quot;bar&quot;}, {id: 3, text: &quot;baz&quot;}]</code></p>
  <p>Basically, I want to retrieve all the posts related to the list of tags.</p>