I want to create a query that will return all Alerts that have a start time before “right now” and an end time after “right now”, and I only care about what time of day it is, not what date it is.
I would have thought this would work
Alert.find(:all, :conditions => [" ? between ? AND ?", Time.now, :start, :end])
However it does not because this query concerns itself with calendar dates, not just time of day.
I found “.to_s(:time)” but when I add that I get an error.
Alert.find(:all, :conditions => [" ? between ? AND ?", Time.now.to_s(:time), :start.to_s(:time), :end.to_s(:time)])
Alert.find(:all, :conditions => [" ? between ? AND ?", Time.now.to_s(:time), :start.to_s(:time), :end.to_s(:time)])
ArgumentError: wrong number of arguments(1 for 0)
from (irb):48:in `to_s'
from (irb):48
from /home/chris/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /home/chris/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /home/chris/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `'
from script/rails:6:in `require'
from script/rails:6:in `'
How can I find all the of alerts that are active right now, based on now being between the start and end time of the Alert, irrespective of the calendar date of the start and end time? I am not currently thinking about time zones but will in the future.
This question represents a bug in ActiveRecord when using sqlite. The solution to this is to switch to mysql. More info here: https://github.com/rails/rails/issues/6247#issuecomment-5679856