title: Rails3 has_many belongs_to scope joins merge isn't working
body: <p>I'm on Rails 3.2, Ruby 1.9.2I am trying to use the scope joins/merge syntax to keep my model logic dry. I am having trouble when testing this tho and I'm not sure why.</p>
  
  <p>product.rb:</p>
  
  <pre><code>belongs_to :product_status
  
  scope :sellable, -&gt; {joins(:product_status).where('products.quantity_available &gt; 0').merge(ProductStatus.sellable)}
  </code></pre>
  
  <p>product_status.rb:</p>
  
  <pre><code>has_many :products
  
  scope :sellable, -&gt; {where("product_statuses.name in ('Active','Deactivated')")}
  </code></pre>
  
  <p>In my tests, testing product status passes:</p>
  
  <pre><code>it "should return 0 count without a valid member" do
    FactoryGirl.create(:product_status)
    ProductStatus.sellable.count.should == 0
  end
  
  it "should return 1 count with a valid member" do
    FactoryGirl.create(:active_product_status)
    ProductStatus.sellable.count.should == 1
  end
  </code></pre>
  
  <p>But I cannot get my product tests to pass:</p>
  
  <pre><code>it "should return 1 count with a valid member" do
    active_product_status = FactoryGirl.create(:active_product_status)
    FactoryGirl.create(:product, :product_status =&gt; active_product_status)
    Product.sellable.count.should == 1
  end
  </code></pre>
  
  <p>The quantity for the product is set to over 0. I've wrote a different scope to just test that part and it passed no problem.</p>
  
  <p>The SQL created by the call looks right to me, but maybe it will show the issue to someone better at this kind of thing:</p>
  
  <pre><code>SELECT COUNT(*) FROM `products` INNER JOIN `product_statuses` ON `product_statuses`.`id` = `products`.`product_status_id` WHERE products.quantity_available &gt; 0 AND (product_statuses.name in ('Active','Deactivated'))
  </code></pre>
  
  <p>I added in this to the test to ensure the active_product_status was in fact sellable</p>
  
  <pre><code>it "should return 1 count with a valid member" do
    active_product_status = FactoryGirl.create(:active_product_status)
    product = FactoryGirl.create(:product, :product_status =&gt; active_product_status)
    ProductStatus.sellable.all.include?(product.product_status).should == true
    Product.sellable.count.should == 1
  end
  </code></pre>
  
  <p>And the ProductStatus test passed, just not the Product one.</p>
  
  <p>I've never used the joins/merge syntax before so I'm not sure if I'm missing something. I can get it to work if I call it from:</p>
  
  <pre><code>ProductStatus.sellable.joins(:products).merge(Product.sellable)
  </code></pre>
  
  <p>but I need to be able to make this call from the product model. Any help would be greatly appreciated.</p>