【问题标题】:`Event.where("start_time >= ?", DateTime.now)` does not work`Event.where("start_time >= ?", DateTime.now)` 不起作用
【发布时间】:2019-03-20 22:11:08
【问题描述】:

我正在向我的 Rails 应用程序添加对重复事件的支持。 其中一个功能是recurring_event.delete_this_and_following

app/models/recurring_event.rb

   16 class RecurringEvent < ApplicationRecord
   17   belongs_to :event

   32   def delete_this_and_following
   33     event.recurring_events.where("start_time >= ?", start_time).destroy_all
   34   end

不幸的是,这给了我这个错误:

(byebug) event.recurring_events.where("start_time >= ?", start_time).size
*** ActiveRecord::StatementInvalid Exception: PG::InFailedSqlTransaction: 
ERROR:  current transaction is aborted, commands ignored until end 
of transaction block
: SELECT COUNT(*) FROM "recurring_events" 
WHERE "recurring_events"."event_id" = $1 AND 
(start_time >= '2018-10-23 10:42:50.281315')

nil

start_date 格式是否错误? start_time.to_s =&gt; "2018-10-23 11:24:59 UTC"

【问题讨论】:

  • start_time 的值是多少? start_time 列的数据类型是什么?
  • start_time => 2018 年 10 月 23 日星期二 11:24:59 UTC +00:00
  • ` t.datetime "start_time"`
  • 试试event.recurring_events.where("recurring_events.start_time &gt;= ?", start_time).destroy_all

标签: ruby-on-rails activerecord


【解决方案1】:

这很奇怪。可能是弹簧未能重新加载或其他原因。 我今天重新启动了我的机器,现在可以了:

event.recurring_events.where("start_time >= ?", start_time).destroy_all

无论如何感谢您的建议! :-)

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 2012-08-09
    • 2020-05-09
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多