为什么这个where子句没有返回正确的记录? - Rails 3

时间:2012-06-03 04:59:41

标签: ruby-on-rails ruby ruby-on-rails-3

=> #<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子句的条件并且仍然返回一个空数组。

2 个答案:

答案 0 :(得分: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'")

答案 1 :(得分:0)

试一试:

Gig.where('date = ?', '2012-06-01 00:00:00')