0

I'm using the following raw Postgres SQL in my where() call to find records where some_date is two days from now:

SomeModel.where('date(some_date) - date(NOW()) = 2')

Can it be written using pure ActiveRecord syntax without raw SQL?

1 Answer 1

2

This should work, haven't tested it though.

SomeModel.where('date(some_date) = ?', 2.days.ago.to_date)
#or
SomeModel.where('some_date::date = ?', 2.days.ago.to_date)

If you want it to be any purer, without the date sql function, I'm not seeing it, at least not without creating a db view.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately this doesn't work with the version of where() that takes a hash. :(
It would be nice but I can live without it. Unless you know an easy way...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.