我有一个ScheduleTime
ActiveRecord对象,time
是column_type :time
。Rails4 - 按时间和/或时间分钟查询?
我试图检索匹配当前的小时和分钟
ScheduleTime.all.select do |s|
now = Time.now
s.time.hour == now.hour && s.time.min == now.min
end
可在此与SQL在where
条款做对象?
编辑:
我已经得到接近,我可以按小时查询:
编辑#2:
这是我目前的执行情况。 ..
class ScheduleTime < ActiveRecord::Base
belongs_to :schedule
validates_presence_of :schedule_id, :time
scope :by_hour, ->(hour) { where("date_part('hour', time) = ?", hour) }
scope :by_minute, ->(minute) {where("date_part('minute', time) = ?", minute) }
scope :by_time, ->(time) { by_hour(time.hour).by_minute(time.min) }
scope :now, -> { by_time(Time.now) }
end
从时间对象中获取分钟的方法是'min'而不是'minute',不是吗? – 2014-09-21 20:00:02