Jeremy Thomas
Jeremy Thomas

Reputation: 6684

Rails query using object's attribute

I have a model, Consultation, and it has two fields :date and :original_date. Is it possible to pull all consultations where :date is greater than one year after :original_date without looping through all consultations?

I tried something like this but it didn't work:

Consultation.where("date >= ?", self.original_date + 1.year)

Upvotes: 1

Views: 105

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14900

If you're using Postgres you can do this

Consultation.where("date >= (original_date + INTERVAL '1 year')")

https://www.postgresql.org/docs/current/functions-datetime.html

Upvotes: 1

Related Questions