您可以使用SQL做了計數,而實例很多Record
對象(它使用的內存/時間)
ActiveRecord::Base.connection.select_rows('SELECT DATE(created_at) AS d, COUNT(*) AS c FROM my_records_table GROUP BY DATE(created_at)')
這會給你一個陣列陣列:
> pp ActiveRecord::Base.connection.select_rows('SELECT DATE(created_at) AS d, COUNT(*) AS c FROM my_records_table GROUP BY DATE(created_at)')
[["2011-06-23", "326"],
["2011-06-24", "337"],
["2011-06-25", "334"],
["2011-06-26", "353"],
["2011-06-27", "210"],
["2011-06-28", "330"],
["2011-06-29", "331"],
["2011-06-30", "338"],
["2011-07-01", "335"],
["2011-07-02", "366"],
["2011-07-03", "320"],
["2011-07-04", "338"],
["2011-07-05", "338"],
["2011-07-06", "299"],
["2011-07-07", "326"],
["2011-07-08", "314"],
["2011-07-09", "331"],
["2011-07-10", "335"],
...但我只看到了德克斯特的答案,這是更好的方式:)
小心寫一些Arel的文檔?也許是一堆Arel單線隊員? –
實際上我偷了它正確的Arel官方文檔:D http://goo.gl/ca9nJ – dexter
感謝您的答覆,但不包括NULL,我需要。你將如何.group()迭代? – Galen