Dave G
Dave G

Reputation: 4386

Problem with dates in Postgresql and Rails

I want to do the following: Find all articles with where Time.now is between article.due_date and article.due_date - 8.hours. I'm having a lot of trouble trying to get it right. Any help would be much appreciated.

Thanks!

Upvotes: 1

Views: 362

Answers (1)

jdl
jdl

Reputation: 17790

Who said that algebra was worthless?

The two conditions you stated in your question.

T <= DUE
T >= DUE - 8h

Are the same as these two conditions.

DUE >= T
DUE <= T + 8h    

You can apply now apply this to a query and/or scope without having to do datetime gymnastics in your database.

named_scope :near_due, lambda {{ :conditions => ["due_date >= ? AND due_date <= ?", Time.now, Time.now + 8.hours] }}

Upvotes: 1

Related Questions