From: Robert Klemme Date: 2009-03-26T01:46:41+09:00 Subject: Re: creating an array by reusing code 2009/3/25 lasitha : > However, this is inefficient.  We have to make 12 passes over the > all_req collection. > You can, instead use Enumerable#group_by: > > all_req.group_by {|r| r.created_at.mon } > > which will return a hash of items keyed by the month.  Getting counts > from this is simply a matter of using #map.  I'll leave this as an > exercise :) Or create a counter Hash and loop once: counts = Hash.new 0 all_req.each {|r| counts[r.created_at.mon] += 1} > Now, having said all that, the most efficient way to do this is > probably to use a group_by clause in your finder.  Again, left as an > exercise (mostly 'cos i don't know AR :) Yep, the database solution is probably the best. Cheers robert -- remember.guy do |as, often| as.you_can - without end