title: Ruby / Rails - Can I use a joined table's scope(or class method) as part of my WHERE clause?
body: <p>I want to grab all the categories that contain purchaseable <code>products</code>.</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
    belongs_to :category
    scope :purchaseable, where(:available =&gt; true)
  end 
  
  class Category &lt; ActiveRecord::Base
    has_many :products
    scope :with_purchaseable_products, ?????
  end
  </code></pre>
  
  <p>So, I'm trying to define <code>:with_purchaseable_products</code>.  This works:</p>
  
  <pre><code>scope :with_purchaseable_products, joins(:products).where("products.available is true").group(:id).having('count(products.id) &gt; 0')
  </code></pre>
  
  <p>But that's not very DRY.  Is there any way to apply my <code>:purchaseable</code> scope to <code>products</code> in my <code>:with_purchaseable_products</code> scope?</p>
  
  <p>Thanks.</p>