1
=> #<Gig id: 59, date: "2012-06-01 00:00:00", title: "Awesome Record", url: "http://someurl.com", body: nil, reply_email: nil, industry_id: nil, created_at: "2012-06-03 03:06:45", updated_at: "2012-06-03 03:06:45"> 


1.9.2-p0 :046 > Gig.where(:date => "2012-06-01 00:00:00")
  Gig Load (0.3ms)  SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = '2012-06-01 00:00:00'
 => [] 
1.9.2-p0 :043 > Gig.where(:date => "2012-06-01")
  Gig Load (0.3ms)  SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = '2012-06-01'
 => [] 
1.9.2-p0 :044 > Gig.where(:date => "June 01")
  Gig Load (0.6ms)  SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = 'June 01'
 => [] 
1.9.2-p0 :045 > Gig.where(:date => "June 1")
  Gig Load (32.9ms)  SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = 'June 1'
 => [] 

我要做的就是在此表中找到具有特定“日期”的记录(甚至是在日期范围内的记录)。但是上面的基本 where 子句不起作用。

我究竟做错了什么?

编辑 1

我将日期输出为字符串,通过to_s这是输出:

d.date
 => Fri, 01 Jun 2012 00:00:00 UTC +00:00 
1.9.2-p0 :079 > d.date.to_s
 => "2012-06-01 00:00:00 UTC" 

不确定这是否会有所帮助....但我什至尝试将这些字符串作为 where 子句的条件,它仍然返回一个空数组。

4

2 回答 2

1

试试这些:

Gig.where(:date => "2012-06-01 00:00:00 +00.00")
Gig.where("date like '2012-06-01%'")
Gig.where("DATE(date) = '2012-06-01'")
于 2012-06-03T05:12:13.913 回答
0

试一把:

Gig.where('date = ?', '2012-06-01 00:00:00')
于 2012-06-03T05:15:49.857 回答