1

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!

1
  • What is it that you are having problems with? Commented Mar 7, 2011 at 18:23

1 Answer 1

1

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] }}
Sign up to request clarification or add additional context in comments.

2 Comments

Ha. I just solved this myself. Had to relookup the rules for inequalities. Probably don't need the lambda unless we're passing the hours, which may indeed be a good idea. Have an accepted answer for your work though :)
You definitely need the lambda, otherwise Time.now will only be evaluated the first time your model is cached.

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.