title: Rails ActiveRecord query using inner join
body: <p>I have two tables that I'm attempting to do an inner join with.</p>
  
  <p>One is a <code>users</code> table where the primary key is <code>id</code>. </p>
  
  <p>Another table is <code>bars</code> where <code>user_id</code> is a foreign key. <code>bars</code> also has a column called <code>foo_id</code> where <code>food_id</code> is a foreign key to the <code>foos</code> table. </p>
  
  <p>I am trying to put together an ActiveRecord query where I can select all users that were created on or before N days ago and do not have any <code>foos</code> where <code>bars.foo_id</code> equal to a particular id. I tried doing something like this:</p>
  
  <pre><code>users = User.where("users.created_at &lt;= ?", 50.days.ago).joins(:bars).where("bars.foo_id != 5")
  </code></pre>
  
  <p>This query fields over 30,000 results, which is incorrect, cause the Users table only has 12,000 rows. </p>
  
  <p>What exactly am I doing wrong here?</p>