Dimillian
Dimillian

Reputation: 3646

Rails, ActiveRecord and date

Stupid question maybe, but it drive me insane right now. Why

 User.where(:created_at => 1.day.ago..Time.now).count
   (0.8ms)  SELECT COUNT(*) FROM "users" WHERE ("users"."created_at" BETWEEN '2012-11-06 17:58:22.443552' AND '2012-11-07 17:58:22.443809')
 => 2 

And

 User.where(:created_at => 1.day.ago.to_date..Time.now.to_date).count
   (0.6ms)  SELECT COUNT(*) FROM "users" WHERE ("users"."created_at" BETWEEN '2012-11-06' AND '2012-11-07')
 => 0 

Give me a different result?

I know that without the time (with .to_date so) it should take midnight for the beginning and the end of the day. This is what I want, but for some obscure reasons, it show me 0, but I've created 2 users like 10 minutes ago....

Upvotes: 0

Views: 716

Answers (1)

HungryCoder
HungryCoder

Reputation: 7616

The following answer may help you. MySQL "between" clause not inclusive?

To be specific to your question, I here copy and rewrite an answer from the above link:

The problem is that 2011-01-31 really is 2012-11-07 00:00:00. That is the beginning of the day. Everything during the day is not included.

So, the entries that you've added 10 minutes ago, are obviously skipped when you did not added the time.

Upvotes: 1

Related Questions