I’m trying to pull a count of all users created in a year, and month but the following doesn’t seem to work as expected.
User.group("YEAR(created_AT), MONTH(created_at)").
count("DISTINCT(id), YEAR(created_at), MONTH(created_at)")
i’m looking for something like
{2011 => {1 => 222, 2 => 333, 4 => 444, 5 => 667 ... }}
but i’m getting
{1 => 222, 2 => 333, 4 => 444, 5 => 667 ... }
Am i missing something, or can ActiveRecord not give me this result in one query?
The
countmethod doesn’t work like you think it does. You end up doing this:That SELECT clause is pretty dodgy but MySQL will, in its usual sloppy manner, let it through. I think you want this query:
I’d probably go straight to
select_alllike this:Or you could do it like this:
Those will give you an array,
a, of Hashes withc,y, andmkeys like this:Then a bit of data wrangling is all you need to finish the job: