Topic: Monthly Stays

I am trying to know how many new users i get each month.
but i am having a problem with the active record group by query


User.count(:all, :conditions => range, :group => "created_at", :order =>"created_at ASC")

I am getting

2013-04-29 16:09:45 UTC => 1,

i need to get 2013-04-29 => 1,

Last edited by moiseszaragoza (2013-04-30 16:44:59)

Re: Monthly Stays

I have a better query if i can call it that

User.group("DATE_TRUNC('month', created_at)").count

it returns

{
    "2012-06-01 00:00:00" => 10,
    "2012-05-01 00:00:00" => 6,
    "2012-07-01 00:00:00" => 10,
    "2012-08-01 00:00:00" => 9,
    "2013-04-01 00:00:00" => 6,
    "2013-03-01 00:00:00" => 6,
    "2013-02-01 00:00:00" => 6,
    "2012-11-01 00:00:00" => 13,
    "2012-10-01 00:00:00" => 5,
    "2012-12-01 00:00:00" => 9,
    "2013-01-01 00:00:00" => 10,
    "2012-09-01 00:00:00" => 10
}

this is exacly what i want it but now i want to sort this by date but i have a string.

Re: Monthly Stays

User.group("DATE(created_at)").order(:created_at).count.sort.each do |date, count|
  puts date.to_date, count
end

Re: Monthly Stays

Thank you

Re: Monthly Stays

You're welcome