【问题标题】:Using Time of Day In Conditions of find while Ignoring Calendar Date with Rails在使用 Rails 忽略日历日期时在查找条件中使用时间
【发布时间】:2012-05-14 18:21:32
【问题描述】:

我想创建一个查询,它将返回开始时间在“现在”之前和结束时间在“现在”之后的所有警报,我只关心它是一天中的什么时间,而不是它的日期是。

我认为这会起作用

Alert.find(:all, :conditions => [" ? between ? AND ?", Time.now, :start, :end])

但不是因为此查询与日历日期有关,而不仅仅是一天中的时间。

我找到了“.to_s(:time)”,但是当我添加它时出现错误。

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 `'

如何根据现在在警报的开始时间和结束时间之间找到所有当前处于活动状态的警报,而不考虑开始和结束时间的日历日期?我目前没有考虑时区,但将来会考虑。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    这个问题代表了使用 sqlite 时 ActiveRecord 中的一个错误。解决这个问题的方法是切换到mysql。更多信息在这里:https://github.com/rails/rails/issues/6247#issuecomment-5679856

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 2013-07-25
      • 2010-11-18
      • 2010-11-02
      • 2012-09-14
      • 1970-01-01
      • 2017-12-07
      • 2018-01-25
      • 2022-07-08
      相关资源
      最近更新 更多