Getting count of elements by `created_at` by day in a given month

前端 未结 3 1971
深忆病人
深忆病人 2021-01-13 19:28

I wanted to make a simple chart of users that have been created in the past month in my app. Like basically for each day in the past month I want to show the count of users

3条回答
  •  旧巷少年郎
    2021-01-13 20:10

    I think separation trumps the minimal performance gains here:

    # 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
    

提交回复
热议问题