4

I am developing a Rails 3.2 web app and in this app I got a query that finds all accounts created more than 2 weeks ago and that paid on a specific date (1 month ago exactly). The problem is that my query does not find anything even though the dates are correct. I think it has something to do with the time. How can I ignore the time in the query and only use the dates?

created_at = Time.now - 2.weeks
paid_at = Time.now - 1.month

Account.where(["created_at >= ? AND paid_at = ?", created_at.beginning_of_day, paid_at.beginning_of_day]).select(:id)

I also tried this with the same result:

Account.where(["created_at >= ? AND paid_at = ?", 2.weeks.ago, 1.month.ago]).select(:id)

I think I am on the right way here but I need to use the created_at part too and it seems to be ignored in this query:

Account.where('created_at > ? AND paid_at BETWEEN ? AND ?', 2.years.ago, 1.month.ago.beginning_of_day, 1.month.ago.end_of_day)
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

1 Answers1

6

Try this with DATE()...

Account.where("DATE(created_at) >= ? AND DATE(paid_at) = ?", created_at, paid_at).select(:id)

see this

Community
  • 1
  • 1
Thaha kp
  • 3,689
  • 1
  • 26
  • 25