【发布时间】:2023-04-02 19:13:01
【问题描述】:
编写返回至少 5 分钟前的记录的 Rails 查询的最佳方法是什么?我正在使用 Rails 3.0.1、Ruby 1.9.2 和 PostgreSQL。目前我有这个:
Note.order('date DESC').where('private_note = ? AND (?-created_at) > 300',false, Time.now.utc)
得到这些错误:
Note Load (0.7ms) SELECT "notes".* FROM "notes" WHERE (private_note = 'f' AND ('2011-09-28 01:07:30.094475'-created_at) > 300) ORDER BY date DESC
PGError: ERROR: operator does not exist: interval > integer
LINE 1: ...'f' AND ('2011-09-28 01:07:30.094475'-created_at) > 300) ORD...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "notes".* FROM "notes" WHERE (private_note = 'f' AND ('2011-09-28 01:07:30.094475'-created_at) > 300) ORDER BY date DESC
Completed in 214ms
ActiveRecord::StatementInvalid (PGError: ERROR: operator does not exist: interval > integer
LINE 1: ...'f' AND ('2011-09-28 01:07:30.094475'-created_at) > 300) ORD...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "notes".* FROM "notes" WHERE (private_note = 'f' AND ('2011-09-28 01:07:30.094475'-created_at) > 300) ORDER BY date DESC):
app/controllers/notes_controller.rb:17:in `public_stream_get_notes'
示例 created_at 值为 2011-09-28 01:00:01 UTC
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord time