title: How to set up this scope with association in Rails?
body: <p>I'm having trouble writing and testing a scope involving a couple of joins and associations.  I'll try to keep my explanation brief but as thorough as possible.</p>
  
  <p>I have the following associations:</p>
  
  <p><code>ExpertTopic &gt; Topic &gt; Articles &gt; Posts</code></p>
  
  <p>and the following code:</p>
  
  <pre><code>class Topic &lt; ActiveRecord::Base
    has_many :articles, :order =&gt; "position", :dependent =&gt; :destroy
    has_many :posts, :through =&gt; :articles
  
    has_many :expert_topic, :dependent =&gt; :delete_all
    has_many :experts, :through =&gt; :expert_topic
  end
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>class ExpertTopic &lt; ActiveRecord::Base
    belongs_to :topic, :inverse_of =&gt; :expert_topic
    belongs_to :expert, :inverse_of =&gt; :expert_topic
  
    scope :live, joins(:topic =&gt; {:articles =&gt; :post})
      .where("topics.article_count &gt; ? AND posts.live = ?", 0, true)
  end
  </code></pre>
  
  <p><strong>With the <code>live</code> scope in <code>ExpertTopic</code>, I'm trying to narrow down to those experts associated with topics with all live posts in them (through articles).</strong></p>
  
  <p>In the Rails console <code>ExpertTopic.live.to_sql</code> is:</p>
  
  <pre><code>"SELECT `experts_topics`.* FROM `experts_topics` INNER JOIN 
  `topics` ON `topics`.`id` = `experts_topics`.`topic_id` INNER JOIN
  `articles` ON `articles`.`topic_id` = `topics`.`id` INNER JOIN
  `posts` ON `posts`.`id` = `articles`.`post_id` WHERE
  (topics.article_count &gt; 0 AND posts.live = 1)"
  </code></pre>
  
  <p>I'm testing my scope with the following code in <code>expert_topic_spec.rb</code>:</p>
  
  <pre><code>describe ExpertTopic do
    before do
      @post1 = FactoryGirl.create(:pending_post)
      @post2 = FactoryGirl.create(:live_post)
      @post3 = FactoryGirl.create(:pending_post)
      @post4 = FactoryGirl.create(:live_post)
      @non_live_topic = FactoryGirl.create(:topic_with_posts, :posts =&gt; [@post1, @post2, @post3])
      @live_topic = FactoryGirl.create(:topic_with_posts, :posts =&gt; [@post2, @post4])
      FactoryGirl.create(:expert_topic, topic_id: @non_live_topic.id)
      FactoryGirl.create(:expert_topic, topic_id: @live_topic.id)
    end
  
    it 'finds and returns only expert with live topic' do
      ExpertTopic.all.count.should == 2
      ExpertTopic.live.uniq.count.should == 1
    end
  end
  </code></pre>
  
  <p><strong>The logic is that since <code>@non_live_topic</code> contains at least one post that's not live it is not considered live and hence shouldn't be returned by a call to <code>ExpertTopic.live</code>.  However, the last assertion fails because <code>ExpertTopic.live.uniq.count</code> returns <code>2</code> instead of <code>1</code>.</strong></p>
  
  <p>I don't know if my scope is written incorrectly or if it's my test, and I'd really appreciate someone's help in debugging!</p>
  
  <p>Thanks!</p>