1

In my ruby on rails application, I wanted to get count of users that have been created and deleted account in the previous day, week and month in my app.Basically I want to show the users that have been created account in one day(means from current hour to previous remaining hours), one week(means from current day to previous remaining days) and one month(means from current day to previous remaining days)

and I am trying use below:

# 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

But it gets me count of users for each day in past month.

Substantial
  • 6,684
  • 2
  • 31
  • 40
user88
  • 1,174
  • 3
  • 27
  • 51
  • http://www.codebeerstartups.com/2013/02/graphical-representation-of-my-model-data-charts-in-ruby-on-rails-using-highcharts/ – Sahil Grover Oct 15 '14 at 06:22
  • http://stackoverflow.com/questions/2381718/rails-activerecord-date-between – John Smith Oct 15 '14 at 06:23
  • but I don't have particular date and time, I want this for one day, from current time`(time.now)` to end where 24 hours is completed, similarly for one week, from current time`(time.now)` to end where one week is completed, and similarly for one month, from current time`(time.now)` to end where one month is completed. – user88 Oct 15 '14 at 06:31

2 Answers2

2

I would try something like this:

For example if you want to selecet the Users created in the last hour:

  @date_start = DateTime.now
  @date_end = @date_start - 1.hour
  @users = User.where(:created_at => @date_end..@date_start)
John Smith
  • 6,105
  • 16
  • 58
  • 109
  • 1
    I want to get count, so I have add `.count` like this `@users = User.where(:created_at => @date_end..@date_start).count`. But its not get me any result. – user88 Oct 15 '14 at 06:44
  • Its not get me any error, but in my view page, at this line `Number of users who registered in last 24 hours: <%= @users %>`, its not show me any count value. – user88 Oct 15 '14 at 06:53
  • Can you simply print it out in the console with: `puts @users` ? What is the result? – John Smith Oct 15 '14 at 06:55
  • Did you create a user in the last hour? – John Smith Oct 15 '14 at 06:58
  • Its working. Thanks, kindly suggest me, how can I do this for `1 Week` and `1 month`. – user88 Oct 15 '14 at 07:03
  • Yes for one week you can keep `@date_start` but you change your `@date_end` to `@date_start - 1.week` same with month simply change it to `@date_end = @date_start - 1.month`. Should work! If you get any errors, simply write back! – John Smith Oct 15 '14 at 07:25
1

let's say first this:

@date = DateTime.now - 10.days
@users = User.all(:conditions => ['created_at > ?', @date]) // fetch all users created within last 10 days until now

then, group it by date:

@days = @users.group_by {|user| user.created_at.to_date }

then you should do iteration with those grouped days and print them:

@days.each do |day, users|
    <%= day %>: <%= users.size %>
end
Marko Krstic
  • 1,417
  • 1
  • 11
  • 13