Can someone explain this?
Post.where(:p_date => ((Time.now - 7.days)..(Time.now))).count
-> 4507
Post.where(:p_date => ((Time.now - 7.days).beginning_of_day..(Time.now).end_of_day)).count
-> 4794
While p_date is only date type without time.
Thank you
If p_date
is a date column, pass dates into your query
Post.where(p_date: (7.day.ago.to_date .. Date.today)).count
which uses this query
SELECT COUNT(*) FROM `posts` WHERE (`posts`.`p_date` BETWEEN '2016-09-08' AND '2016-09-15')
If you pass in time objects, the database will automatically compare against 0:00:00 time (at least on mysql and postgres) which is where you get your discrepancy from.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.