title: Complex Rails query across multiple tables
body: <p>I have a table of <code>Postcodes</code> which is joined to a table of <code>Companies</code>.
  There is a join table <code>CompaniesPostcode</code>.</p>
  
  <p>I want to take the <code>postcode</code> someone enters, search the join table for matches, and return the <code>company_ids</code>.</p>
  
  <p><em>Then</em>, I want to pass this query into another table (called <code>company_selected_categories</code>) to find what categories the company has selected, and create a count of each <code>category_id</code>.</p>
  
  <p>I then want to render from each <code>category_id</code> the <code>name</code>, <code>image</code> and count of how many <code>partners</code> - and only render if this count is > 0.</p>
  
  <p>More info on my data modelling can be found <a href="https://gist.github.com/pressdapp/e96bd76cbf70b4e100c03bc728d3c5ff" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Does anyone know how I could achieve this?</p>