wk220
wk220

Reputation: 23

Search between dates with a variable interval w/ rails

I am wondering how this can be handled via a db query instead of looking up all of the sites.

Site.all.select { |x| x.last_ran <= DateTime.now - x.interval.minutes }

Basically i need to find all the sites that were last_ran before their interval. So if i ran my site at 12:00 and its 12:06 but the interval is 5 minutes, then it would be returned by this query.

The problem im having is converting x.interval.minutes (5.minutes) at the database level.

Thanks - Also im using postgres if there a db specific way to do this!

Upvotes: 2

Views: 144

Answers (1)

nzifnab
nzifnab

Reputation: 16092

In postgres this would work...

Site.where(
  "sites.last_ran <= CURRENT_TIMESTAMP - sites.interval * INTERVAL '1 second'"
)

I think you could use either NOW() or CURRENT_TIMESTAMP

Upvotes: 1

Related Questions