2011-03-24 96 views
0

我有屬於位置爲什麼會這樣返回NULL

>> r = Request.find 11210 
=> #<Request id: 11210, artist: "Coldplay", song: "Amsterdam", venue: "someplace", showdate: "2011-02-23", amount: nil, user_id: 11, created_at: "2011-02-23 02:55:13", updated_at: "2011-03-24 02:55:13", like_reason: "Its Great", pledge: #<BigDecimal:1032f1850,'0.29E2',9(18)>, location_id: 243, charge_card: false, approval_key: nil, charged: false, paypal_email: "[email protected]"> 
>> r.location.showdate 
=> Wed, 23 Feb 2011 
>> r.showdate 
=> Wed, 23 Feb 2011 
>> Request.find_all_by_showdate(r.location.showdate) 
=> [] 
>> 

他們在DB兩個日期字段這個請求......

我目前使用SQLite如果有差別

+0

如果您使用的是Rails3,Request.find_all_by_showdate(r.location.showdate).to_sql的輸出是什麼 – chris 2011-03-24 03:17:01

回答

0

在處理日期/日期時間時,我通常會用.to_s(:db)格式化日期,以便在查詢中很好地發揮作用。我不確定這是否是你問題的根源,但那是我開始的地方。

0

如果你從Request.find 11210「原始」的輸出,你會看到這一點:

showdate: "2011-02-23" 

誠然,這不是r.location.showdate但它應該給你一個提示。試試這個:

Request.find_all_by_showdate('2011-02-23') 

如果這樣的話,那麼你只是有一個格式問題。

請記住,SQLite stores everything as a string在內部,它接受列定義中的常用數據類型,但內部都是字符串;因此,沿線某處,SQLite的司機會說:

r.location.showdate.to_s 

,並得到

'Wed, 23 Feb 2011' 

然後,它會尋找在showdate列字符串,但它不會找到它,因爲showdate包含日期ISO-8601 format