我需要计算上周每天创建的用户数量。我创造了这样的东西:
users = User.where(created_at: (Time.now - 7.day)..Time.now)
users.group_by{|p| p.created_at.at_beginning_of_day}.each do |day, array|
puts "on #{day} #{array.size}"
end
我想输出为:
on Sun 29
on Mon 34
etc.
但是我得到了这个:
{Sun, 25 Aug 2013 00:00:00 CEST +02:00=>[#<User id: 10, email: "jssdc@ssds.cz", encrypted_password: "$2a$10$QWz7p86JAURxvWJj8GZFm.o2a589y7T4htcOJyUqe4aK...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-25 12:30:05", updated_at: "2013-08-25 12:30:40", authentication_token: "ytMoqBwRHVZseN7bbsvU", confirmation_token: "fuWEN7DGhC6XpRqtSpWR", confirmed_at: nil, confirmation_sent_at: "2013-08-25 12:30:05", unconfirmed_email: nil>], Tue, 27 Aug 2013 00:00:00 CEST +02:00=>[#<User id: 11, email: "bz@sefdsa.cz", encrypted_password: "$2a$10$4ZZq58B.az9vZ1TFaNVkVeN7PyLRogKEilpV8VvXFY1u...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-27 21:44:06", updated_at: "2013-08-27 21:44:06", authentication_token: "A1MPhxUQLsPDNSo25xUK", confirmation_token: "sipxKTmuNNvnp4RePsYd", confirmed_at: nil, confirmation_sent_at: "2013-08-27 21:44:06", unconfirmed_email: nil>], Wed, 28 Aug 2013 00:00:00 CEST +02:00=>[#<User id: 12, email: "fdsafdas@sfdsa.cz", encrypted_password: "$2a$10$y/RGxp5dpwq.jpV2d9HTAOLgLFjqgtJnS6IWpFyFwxLj...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-27 23:00:43", updated_at: "2013-08-27 23:00:43", authentication_token: "UE9ty2K5EoRgm4oveMhf", confirmation_token: "a7186dyyyVNMyBpTpJ6k", confirmed_at: nil, confirmation_sent_at: "2013-08-27 23:00:43", unconfirmed_email: nil>, #<User id: 13, email: "madafaka@lol.cz", encrypted_password: "$2a$10$2yDV5KNs3JMpP.OJ/f6rQOgu6P6DFS/Xoc33eYtlAJ6z...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-27 23:58:51", updated_at: "2013-08-27 23:58:51", authentication_token: "fieiR4ZJ9y9PWux82CLq", confirmation_token: "9ujvWvbvnubyZWisM4sy", confirmed_at: nil, confirmation_sent_at: "2013-08-27 23:58:51", unconfirmed_email: nil>]}
如何以所需格式输出上周创建的用户?
答案 0 :(得分:3)
User.group("date(created_at)").count
您将获得类似
的输出{"2013-08-27"=>1, "2013-08-13"=>2, "2013-08-19"=>2}
为了获得一周的计数,你可以做到,
User.where("created_at > ?", 7.days.ago).group("extract(dow from created_at)").count
这将为您提供类似
的输出{"2" => 2, "0" => 1}
“0”是星期日,“1”是星期一,依此类推
答案 1 :(得分:0)
对于MySQL,这可能会起作用:
User.where(created_at: 7.days.ago..Time.now)
.group("LEFT(DAYNAME(created_at), 3)").count.each do |day, count|
puts "on #{day}: #{count}"
end