limitをチェーンしたときのcountの戻り値に注意

Failures:

  1) CampaignsQuestion#answers 最新回答一覧が取得できること
     Failure/Error: @answers[:latest].size.should eq 15
       
       expected: 15
            got: 30
       
       (compared using ==)
     # ./spec/models/campaigns_question_spec.rb:58:in `block (3 levels) in <top (required)>'
 describe "#default_comments" do
    before do
      content = Factory(:content)      
      15.times{Factory(:comment,:question => content)}

      @content_comments = content.default_comments
    end
    
    it '最新回答一覧が取得できること' do
      @answers[:latest].size.should eq 15
    end

  end

contentオブジェクトは1311のcommentオブジェクトを持っている

content = Content.find(1)
content.count
=> 1311

limit(10)でcommentの数が10であることを期待するが、戻り値は1311となる。

content.comment.limit(10).count
=> 1311

しかし、sqlを確認すると、10件取得するクエリができている。

content.comments.limit(10).to_sql
=> "SELECT  `comments`.* FROM `comments` WHERE (`comments`.content_id = 1 ) LIMIT 10"