Periods before the first one or after the last one where the count is positive are not included?
1990eam opened this issue · comments
I apologize if there's an option or workaround for this.
I'm working with the following statement:
Model.where(created_at: (Date.current - 20.days)..Date.current).group_by_day(:created_at, default_value: 0).count
In the results hash I spot the following:
-
The first day in the hash seems to be the first day within the range where the count throws a positive value
-
The last day in the hash seems to be the last day within the range where the count is positive as well.
All days within the first day with a positive value and the last day with a positive value where the count is 0 are included with the default_value
(this is fine).
However Date.current - 20.days
goes all the way back to May 17th and Date.current
is June 6th. Those days throw a count of 0, and are not included.
E.g:
{
Fri, 19 May 2023=>1,
Sat, 20 May 2023=>0,
Sun, 21 May 2023=>0,
Mon, 22 May 2023=>0,
Tue, 23 May 2023=>2,
Wed, 24 May 2023=>1,
Thu, 25 May 2023=>0,
Fri, 26 May 2023=>0,
Sat, 27 May 2023=>0,
Sun, 28 May 2023=>0,
Mon, 29 May 2023=>0,
Tue, 30 May 2023=>0,
Wed, 31 May 2023=>6,
Thu, 01 Jun 2023=>0,
Fri, 02 Jun 2023=>1,
Sat, 03 Jun 2023=>0,
Sun, 04 Jun 2023=>0,
Mon, 05 Jun 2023=>1
}
Expected:
{
Wed, 17 May 2023=>0,
Thu, 18 May 2023=>0,
Fri, 19 May 2023=>1,
Sat, 20 May 2023=>0,
Sun, 21 May 2023=>0,
Mon, 22 May 2023=>0,
Tue, 23 May 2023=>2,
Wed, 24 May 2023=>1,
Thu, 25 May 2023=>0,
Fri, 26 May 2023=>0,
Sat, 27 May 2023=>0,
Sun, 28 May 2023=>0,
Mon, 29 May 2023=>0,
Tue, 30 May 2023=>0,
Wed, 31 May 2023=>6,
Thu, 01 Jun 2023=>0,
Fri, 02 Jun 2023=>1,
Sat, 03 Jun 2023=>0,
Sun, 04 Jun 2023=>0,
Mon, 05 Jun 2023=>1
Tue, 06 Jun 2023=>0
}
Is there an option or workaround for this? I have the same issue grouping by month, etc.
Thanks.
Hi @1990eam, use the range
option instead of where(...)
to get the entire series.