Just like the title asks, how do I include a date range in a query?
For example, let's say I have a model called VisitToGrandma and it has a field called visit_date. Now, I want to find all visits to grandmothers that happened in the last month.
Here is what I'm trying:
VisitToGrandma.where("? @> visit_date", (1.month.ago..Time.now))
However, this produces the query:
SELECT "visits_to_grandmas".* FROM "visits_to_grandmas" WHERE ('2018-07-01','2018-07-02','2018-07-03','2018-07-04','2018-07-05','2018-07-06','2018-07-07','2018-07-08','2018-07-09','2018-07-10','2018-07-11','2018-07-12','2018-07-13','2018-07-14','2018-07-15','2018-07-16','2018-07-17','2018-07-18','2018-07-19','2018-07-20','2018-07-21','2018-07-22','2018-07-23','2018-07-24','2018-07-25','2018-07-26','2018-07-27','2018-07-28','2018-07-29','2018-07-30','2018-07-31','2018-08-01' @> visit_date)
What's the correct way to parameterize this?
Yes, I know for this fictional example, I can just use the start and end date and use BETWEEN or some other operators to do this without using an actual date range, but that's not what I'm asking about.
Edit to point out how this question is different from the other question:
That question is just about finding a date in a range, my question is asking how to actually parameterize a range object so that all of postgresql's daterange operators can be used.
VisitToGrandma.where(visit_date: 1.month.ago..Time.now)?daterange?