0

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....

Dimillian
  • 3,616
  • 4
  • 33
  • 53

1 Answers1

1

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.

Community
  • 1
  • 1
HungryCoder
  • 7,506
  • 1
  • 38
  • 51
  • Thank you, now I use Time.now.beginning_of_day etc... without converting it to date :) So my results are accurate. – Dimillian Nov 08 '12 at 16:28