When I create a SQL Time column I get a pure time, ie. no date. But when I use the Ruby Time class I get a date as well. The problem is when do a 'find' the generated SQL includes a date and I seem to be getting weird results.
当我创建一个SQL时间列时,我得到了一个纯粹的时间。没有日期。但是当我使用Ruby时间类时,我也会得到一个约会。问题是,当执行“查找”时,生成的SQL包含一个日期,而我似乎得到了奇怪的结果。
The Table
表
start_date: time
end_time: time
day_of_week: string
ActiveRecord
ActiveRecord
def self.now_playing
self.find(:first, :conditions => ['day_of_week = ? AND end_time > ? AND start_time < ?', Time.now.strftime('%A'), Time.now, Time.now])
end
The SQL
SQL
SELECT * FROM `schedules` WHERE (day_of_week = 'Saturday' AND end_time > '2009-06-20 10:19:59' AND start_time < '2009-06-20 10:19:59') LIMIT 1
The SQL generated includes a date, could this be why I'm getting odd results, for example no record returned when there is a schedule for the given time? However if the column is a pure time column should MySQL not ignore the date part?
生成的SQL包含一个日期,这可能是我得到奇怪结果的原因吗,例如当给定时间有一个调度时,没有返回记录?但是,如果列是纯时间列,MySQL不应该忽略日期部分吗?
1 个解决方案
#1
3
Time in Ruby is more like Timestamp... always has a date. ActiveSupport has built-in conversions for this, so if the date is screwing up your results, you can use #to_s(:format) to get the results you need. By default, there is a :time option, but it only returns hours / minutes (%H:%M) , if you needed seconds as well you'd have to add another format ...
在Ruby中时间更像是时间戳…总是有一个约会。ActiveSupport有内置的转换功能,所以如果日期把结果搞砸了,可以使用#to_s(:format)获得所需的结果。默认情况下,有一个:time选项,但是它只返回小时/分钟(%H:%M),如果您也需要秒,您需要添加另一种格式……
Create an initializer called time_formats.rb or something and add:
创建一个名为time_formats的初始化器。rb或其他什么,加上:
Rails 2-ish
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!({
:time_long => "%H:%M:%S"
})
Rails 3
Time::DATE_FORMATS.merge!(
:time_long => "%H:%M:%S"
)
Then in your finder, just do like so:
然后在你的发现者中,这样做:
def self.now_playing
time = Time.now
self.find(:first, :conditions => ['day_of_week = ? AND end_time > ? AND start_time < ?', time.strftime('%A'), time.to_s(:time_long), time.to_s(:time_long)])
end
More info can be found here: http://jasonseifer.com/2010/03/10/rails-date-formats
更多信息可以在这里找到:http://jasonseifer.com/2010/03/10/rails-date-format。
Or here: http://brian.rarevisions.net/extending-date-formats-in-rails-3
或者在这里:http://brian.rarevisions.net/extending-date-formats-in-rails-3
#1
3
Time in Ruby is more like Timestamp... always has a date. ActiveSupport has built-in conversions for this, so if the date is screwing up your results, you can use #to_s(:format) to get the results you need. By default, there is a :time option, but it only returns hours / minutes (%H:%M) , if you needed seconds as well you'd have to add another format ...
在Ruby中时间更像是时间戳…总是有一个约会。ActiveSupport有内置的转换功能,所以如果日期把结果搞砸了,可以使用#to_s(:format)获得所需的结果。默认情况下,有一个:time选项,但是它只返回小时/分钟(%H:%M),如果您也需要秒,您需要添加另一种格式……
Create an initializer called time_formats.rb or something and add:
创建一个名为time_formats的初始化器。rb或其他什么,加上:
Rails 2-ish
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!({
:time_long => "%H:%M:%S"
})
Rails 3
Time::DATE_FORMATS.merge!(
:time_long => "%H:%M:%S"
)
Then in your finder, just do like so:
然后在你的发现者中,这样做:
def self.now_playing
time = Time.now
self.find(:first, :conditions => ['day_of_week = ? AND end_time > ? AND start_time < ?', time.strftime('%A'), time.to_s(:time_long), time.to_s(:time_long)])
end
More info can be found here: http://jasonseifer.com/2010/03/10/rails-date-formats
更多信息可以在这里找到:http://jasonseifer.com/2010/03/10/rails-date-format。
Or here: http://brian.rarevisions.net/extending-date-formats-in-rails-3
或者在这里:http://brian.rarevisions.net/extending-date-formats-in-rails-3