Why does SQL Server ActiveRecord contain Date for a Time columns?

When I create the SQL Time column, I get pure time, i.e. without date. But when I use the Ruby Time class, I get the date as well. The problem is when the “find” generated SQL includes the date, and I seem to get weird results.

Table

start_date: time
end_time: time
day_of_week: string

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

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 generated SQL contains the date, maybe that's why I get odd results, for example, the record is not returned when there is a schedule at the moment? However, if the column is a pure time column, if MySQL does not ignore the date part?

+3
source share
1 answer

Ruby Timestamp... . ActiveSupport , , , #to_s (: format), . : time, / (% H:% M), , ...

time_formats.rb - :

Rails 2-ish

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!({
  :time_long => "%H:%M:%S"
})

3

Time::DATE_FORMATS.merge!(
  :time_long => "%H:%M:%S"
)

:

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

: http://jasonseifer.com/2010/03/10/rails-date-formats

: http://brian.rarevisions.net/extending-date-formats-in-rails-3

+3

Source: https://habr.com/ru/post/1710898/


All Articles