我想创建迷你图来说明过去 2 周在我的博客上创建的帖子数量。为此,我需要首先生成一个数组,其中包含相关期间每天创建的帖子数。
例如,这个数组:
[40, 18, 0, 2, 39, 37, 22, 25, 30, 60, 36, 5, 2, 2]
生成这个迷你图:(我在 Google Charts API周围使用 Googlecharts 包装器)
我的问题是如何创建这些数组。这是我现在正在做的事情:(我正在使用Searchlogic进行查询,但即使您从未使用过它也应该可以理解)
history = []
14.downto(1) do |days_ago|
history.push(Post.created_at_after((days_ago + 1).day.ago.beginning_of_day).created_at_before((days_ago - 1).days.ago.beginning_of_day).size)
end
这种方法既丑陋又缓慢——一定有更好的方法!