title: Rails includes() LEFT OUTER JOIN with custom ON clause
body: <p>In my scenario, I have a <code>BlogPost</code> model that <code>has_and_belongs_to_many :categories</code>. I want to filter <em>out</em> Blog Posts that belong to specific Categories, while still allowing for uncategorized Blog Posts (hence the need for a LEFT OUTER JOIN).</p>
  
  <p>I expected this to work:</p>
  
  <pre><code>BlogPost.active.includes(:categories).where.not(categories: { id: [1, 2, 3] })
  </code></pre>
  
  <p>But it doesn't filter correctly because it puts the conditions at the end of the query, outside the LEFT OUTER JOIN (see this SO question / answer for the reason: <a href="https://stackoverflow.com/questions/354070/sql-join-where-clause-vs-on-clause?answertab=active#tab-top">SQL join: where clause vs. on clause</a>)</p>
  
  <hr>
  
  <p>This works, but it is ugly:</p>
  
  <pre><code>BlogPost.active.joins("LEFT OUTER JOIN blog_posts_categories
        ON blog_posts_categories.blog_post_id = blog_posts.id
        AND blog_posts_categories.category_id NOT IN(1, 2, 3)")
  </code></pre>
  
  <hr>
  
  <p>Is there an ActiveRecord-friendly way to add conditionals to the ON clause of a LEFT OUTER JOIN without manually typing it out the way I did?</p>