ltdev
ltdev

Reputation: 4457

Get records within specific year and month

I'm having this class method for filtering records that belong to specific year and month

@posts = Post.filtered(params).published

  def self.filtered (params)
    unless params[:year].blank? && params[:month].blank?
      year = params[:year].to_i
      month = params[:month].to_i
      return where(created_at: Date.new(year, month, 1)..Date.new(year, month, -1))
    end
    self
  end

Which generates:

SELECT  `posts`.* 
FROM `posts` 
WHERE (`posts`.`created_at` BETWEEN '2017-09-01' AND '2017-09-30') -- AND ...

In rails c I did Post.pluck('id, created_at') and get these results

SELECT id, created_at FROM `posts` ORDER BY created_at DESC
=> [[21, Fri, 06 Oct 2017 22:10:00 UTC +00:00],
 [22, Sat, 30 Sep 2017 22:10:00 UTC +00:00], # September records
 [7, Fri, 08 Sep 2017 19:48:14 UTC +00:00],  # is where I get wrong number of records
 [4, Tue, 29 Aug 2017 20:55:19 UTC +00:00],
 [9, Wed, 16 Aug 2017 02:32:24 UTC +00:00],
 [19, Tue, 15 Aug 2017 09:57:58 UTC +00:00],
 [14, Mon, 14 Aug 2017 20:03:49 UTC +00:00],
 [18, Mon, 14 Aug 2017 16:04:40 UTC +00:00],
 [2, Sat, 12 Aug 2017 15:44:21 UTC +00:00],
 [15, Sun, 06 Aug 2017 22:36:10 UTC +00:00],
 [10, Sat, 22 Jul 2017 23:17:41 UTC +00:00],
 [12, Fri, 21 Jul 2017 06:38:29 UTC +00:00],
 [17, Sat, 15 Jul 2017 06:49:25 UTC +00:00],
 [1, Fri, 14 Jul 2017 22:18:51 UTC +00:00],
 [5, Tue, 11 Jul 2017 03:31:57 UTC +00:00],
 [16, Sun, 09 Jul 2017 06:17:50 UTC +00:00],
 [13, Sat, 08 Jul 2017 09:49:45 UTC +00:00],
 [20, Sat, 08 Jul 2017 06:34:16 UTC +00:00],
 [3, Thu, 06 Jul 2017 00:12:23 UTC +00:00],
 [8, Sun, 02 Jul 2017 20:28:49 UTC +00:00],
 [11, Fri, 23 Jun 2017 04:03:01 UTC +00:00],
 [6, Wed, 21 Jun 2017 07:02:12 UTC +00:00]]

I have tried filtering by all records for each month however the only one I'm getting wrong are records belong in September, where I get only one record instead of two.

Upvotes: 1

Views: 205

Answers (2)

Scott Monceaux
Scott Monceaux

Reputation: 359

Try...

def self.filtered(params)
  if params[:year].present? && params[:month].present?
    year = params[:year].to_i
    month = params[:month].to_i
    return where(created_at: Date.new(year, month, 1)..Date.new(year, month + 1, 1).to_time - 1.second)
  end
  self
end

All I did was move your end date to the beginning of the next month so you are looking for greater than Sept 1 at midnight and less than Oct 1 at midnight.

Upvotes: 1

Blag
Blag

Reputation: 5894

Select * from dual 
where created_at BETWEEN '2017-09-01' AND '2017-09-30'

Can be read as :

Select * from dual 
where created_at >= '2017-09-01 00:00:00' 
    AND created_at <= '2017-09-30 00:00:00'

And 30 Sep 2017 22:10:00 is not under or equal at 2017-09-30 00:00:00...

edit :

SQL Fiddle

MySQL 5.6 Schema Setup:

CREATE TABLE t
    (`d` datetime)
;

INSERT INTO t
    (`d`)
VALUES
    ('2017-09-30 00:00:00'),
    ('2017-09-30 02:02:02'),
    ('2017-09-20 12:12:12'),
    ('2017-09-08 21:21:21'),
    ('2017-09-08 00:00:00')
;

Query 1:

select date_format(d,'%Y-%m-%d %k:%i:%s') h from t
where d between '2017-09-08' and '2017-09-30'
order by d

Results:

|                   h |
|---------------------|
|  2017-09-08 0:00:00 |
| 2017-09-08 21:21:21 |
| 2017-09-20 12:12:12 |
|  2017-09-30 0:00:00 |

Query 2:

select date_format(d,'%Y-%m-%d %k:%i:%s') h from t
where d between '2017-09-08' and '2017-09-30 23:59:59'
order by d

Results:

|                   h |
|---------------------|
|  2017-09-08 0:00:00 |
| 2017-09-08 21:21:21 |
| 2017-09-20 12:12:12 |
|  2017-09-30 0:00:00 |
|  2017-09-30 2:02:02 |

Upvotes: 1

Related Questions