为什么会这样返回NULL

问题描述:

我有属于位置为什么会这样返回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

在处理日期/日期时间时,我通常会用.to_s(:db)格式化日期,以便在查询中很好地发挥作用。我不确定这是否是你问题的根源,但那是我开始的地方。

如果你从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