日期时间范围内的语法错误Ruby on Rails中的查询

时间:2015-07-13 10:10:23

标签: ruby-on-rails ruby postgresql datetime

我正在执行ruby查询:

ResourceFileName.Variable1 //it doesn't show up.

我收到以下错误

return ResourceFileName.Variable1 // doesn't work.

我想这是因为我在周三之后没有逃脱','字符。我有两个问题:

1)如何更正此查询? 2)我将使用实际日期时间变量替换周三,2015年7月1日18:00:00 SGT +08:00?如果日期时间是变量而不是常量,我如何以正确的方式键入此查询?

4 个答案:

答案 0 :(得分:1)

我会写这样的东西:

Timeslot.where(
  'start_date >= ? AND end_date <= ?', 
  Date.parse('Wed, 01 Jul 2015 18:00:00 SGT +08:00'),
  Date.parse('Wed, 01 Jul 2015 21:00:00 SGT +08:00')
)

答案 1 :(得分:0)

似乎您必须将字符串日期转换为正确的日期格式以执行查询操作。就像你在UTC时区中有日期一样,那么最好使用Time对象并转换为正确的日期格式并在查询中使用它,

str = "Tue, 10 Aug 2010 01:20:19 +0400"
puts Date.parse str
2010-08-10

puts Date.parse(Time.parse(str).utc.to_s)
2010-08-09

答案 2 :(得分:0)

你输入参数Wed,01 Jul 2015 18:00:00 SGT +08:00没有引号?

我想如果你想使用那种格式。试试这个。

"Wed, 01 Jul 2015 18:00:00 SGT +08:00".to_time()
# to_time() method converted to Time Class (timezone is followed by rails setting)

"2015-07-01 18:00:00".to_time() # works well.

答案 3 :(得分:0)

USE DATETIME ...

start_date=DateTime.parse(Wed, 01 Jul 2015 18:00:00 SGT +08:00)
end_date=DateTime.parse(Wed, 01 Jul 2015 21:00:00 SGT +08:00)
Timeslot.where("start_date >= ? AND end_date <= ?",start_date,end_date)