2012-01-05 65 views
0

查看此查詢有何錯誤?如何查詢最近幾周創建的記錄? ...現在至2周前

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

它沒有返回有效記錄?下面是導致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') 

應該尋找記錄:

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

感謝

回答

4

只需翻轉你的時間範圍內,它應該工作

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