在我的ruby on rails应用程序中,我想获得在我的应用程序中前一天、前一周和前一个月创建和删除帐户的用户数。基本上,我想显示在一天(means from current hour to previous remaining hours)
、一周(means from current day to previous remaining days)
和一个月(means from current day to previous remaining days)
中创建帐户的用户
我正在尝试使用以下内容:
# Controller
@users = User.all(:conditions => ["created_at >= ?", Date.today.at_beginning_of_month])
# View
Date.today.at_beginning_of_month.upto(Date.today).each do |date|
<%= date %>: <%= @users.select{|u| u.created_at == date }.size %>
end
但它让我统计了过去一个月里每天的用户数。
我会尝试这样的东西:
例如,如果您想选择在最后一小时内创建的用户:
@date_start = DateTime.now
@date_end = @date_start - 1.hour
@users = User.where(:created_at => @date_end..@date_start)
让我们先说一下:
@date = DateTime.now - 10.days
@users = User.all(:conditions => ['created_at > ?', @date]) // fetch all users created within last 10 days until now
然后,按日期分组:
@days = @users.group_by {|user| user.created_at.to_date }
然后你应该对这些分组的日子进行迭代并打印出来:
@days.each do |day, users|
<%= day %>: <%= users.size %>
end