Rails eager loading of counts?

后端 未结 5 1030
暖寄归人
暖寄归人 2021-02-02 14:50

Eager loading is nice with the include attribute

Post.find(:all, :include => :author)

I\'m wondering if you can also eager load counts, like

5条回答
  •  无人共我
    2021-02-02 15:17

    Try this:

    Comment.count(:group => :post)
    

    To filter by conditions:

    Comment.count(:group => :post, :conditions => {:approved => true })
    

    These will return hashes with posts as keys and the number of comments as values.

提交回复
热议问题