我有一系列日期
[Thu, 06 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Mon, 10 Feb 2014, Tue, 11 Feb 2014, Wed, 12 Feb 2014, Wed, 12 Feb 2014, Wed, 12 Feb 2014, Thu, 13 Feb 2014, Thu, 13 Feb 2014, Fri, 14 Feb 2014, Fri, 14 Feb 2014, Fri, 14 Feb 2014, Fri, 14 Feb 2014, Fri, 14 Feb 2014, Fri, 14 Feb 2014, Fri, 14 Feb 2014]
这是如此创建的:
votes.find(:all, order: :created_at).map{|v| v.created_at.to_date }
如何计算每天创建的票数?
答案 0 :(得分:0)
我认为您可以使用sql查询直接执行此操作:
Vote.order(:created_at).group("date(created_at)").count
此外:
Vote.order(:created_at).group("to_char(created_at, 'YYYY/MM/DD')").count
答案 1 :(得分:0)
为什么不
Votes.order("to_char(created_at,'MM/DD/YYYY')").group("to_char(created_at,'MM/DD/YYYY')").select("to_char(created_at,'MM/DD/YYYY') as creation, Count(votes.id) as vote_count")
这将返回一个ActiveRecord :: Relation,其中包含创建日期MM / DD / YYYY(您显然可以更改此格式以匹配您的'Dy, DD Mon YYYY'
)和该日期的投票数。可以通过方法creation
和vote_count