AnApprentice
AnApprentice

Reputation: 111030

How to query for records that were created in the last weeks? ... Between now and 2 weeks ago

See anything wrong with this query?

time_range = (Time.now..2.weeks.ago)
access_code = group.group_access_codes.where(:group_access_codes => {:uuid => code, :created_at => time_range}).first

It's not returning a valid record? Here is the resulting SQL:

SELECT "group_access_codes".* 
FROM "group_access_codes" 
WHERE "group_access_codes"."uuid" = 'ae342d4a13' 
    AND ("group_access_codes".group_id = 391)
    AND ("group_access_codes"."created_at" BETWEEN '2012-01-05 00:53:34.811469' AND '2011-12-22 00:53:34.811521') 

The record it should be finding:

created_at = 2012-01-05 00:36:41.710613

Thanks

Upvotes: 0

Views: 1295

Answers (1)

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

Just flip your time range and it should work

time_range = (2.weeks.ago..Time.now)
access_code = group.group_access_codes.where(:group_access_codes => {:uuid => code, :created_at => time_range}).first

Upvotes: 4

Related Questions