ruby-on-rails – 获取一个数组,其中包含过去两周内创建的帖子数
发布时间:2020-12-17 03:43:53 所属栏目:百科 来源:网络整理
导读:我想创建迷你图,说明过去两周内在我的博客上创建的帖子数量.为此,我需要首先生成一个数组,其中包含在相关时段内每天创建的帖子数. 例如,这个数组: [40,18,2,39,37,22,25,30,60,36,5,2] 生成这个迷你线:(我正在使用Googlecharts wrapper around the Google C
我想创建迷你图,说明过去两周内在我的博客上创建的帖子数量.为此,我需要首先生成一个数组,其中包含在相关时段内每天创建的帖子数.
例如,这个数组: [40,18,2,39,37,22,25,30,60,36,5,2] 生成这个迷你线:(我正在使用Googlecharts wrapper around the Google Charts API) 我的问题是如何创建这些数组.这就是我现在正在做的事情:(我正在使用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 这种方法既丑陋又缓慢 – 必须有更好的方法! 解决方法
这将为您提供一个哈希映射日期以发布计数:
counts = Post.count( :conditions => ["created_at >= ?",14.days.ago],:group => "DATE(created_at)" ) 然后,您可以将其转换为数组: counts_array = [] 14.downto(1) do |d| counts_array << (counts[d.days.ago.to_date.to_s] || 0) end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读