From 329f39a37d1316ca818f742b5407f5d6dc4ae258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=A3is=20Ozols?= Date: Fri, 3 Dec 2010 13:25:58 +0200 Subject: Optimised post_count method. See gist for benchmarks - https://gist.github.com/726844 --- app/models/blog_category.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'app') diff --git a/app/models/blog_category.rb b/app/models/blog_category.rb index 6d5e886..8ffe834 100644 --- a/app/models/blog_category.rb +++ b/app/models/blog_category.rb @@ -8,15 +8,8 @@ class BlogCategory < ActiveRecord::Base has_friendly_id :title, :use_slug => true - # this might be able to be optimised a little more def post_count - count = 0 - - self.posts.each do |p| - count += 1 if p.live? - end - - count + posts.select(&:live?).count end end -- cgit v1.2.3