我需要获取在特定时间段内创建的记录的数量。我现在尝试使用的内容看起来有点像这样:
User.where('created_at between ? and ?', start, finish).group("date(created_at)").count我得到的是以日期为键,以数字为值的散列。但是,当特定日期的值为0时,它不会包含在散列中。如何才能将这些日期也包括在内?我的意思是MySQL,而不是Ruby语言,我希望它尽可能快。
发布于 2015-12-14 22:42:21
如果数据库中在给定范围内没有记录,则AR或MySQL无法创建组。
我had a similar issue和我只能用Ruby..
User
.where('created_at between ? and ?', start, finish)
.group("date(created_at)")
.flat_map{ |a,b| [a => b.count] }
.inject(:merge)https://stackoverflow.com/questions/34268771
复制相似问题