如何修复此 Rails AR 查询以获取至少 5 分钟前的记录?

发布于 2024-12-06 20:47:27 字数 1514 浏览 1 评论 0原文

编写返回至少 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

What is the best way to write a Rails query that returns records that are at least 5 minutes old? I am using Rails 3.0.1, Ruby 1.9.2 and PostgreSQL. At the moment I have this:

Note.order('date DESC').where('private_note = ? AND (?-created_at) > 300',false, Time.now.utc)

which gets these errors:

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'

A sample created_at value is 2011-09-28 01:00:01 UTC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-12-13 20:47:27
Note.where('created_at <= ?', 5.minutes.ago).where(:private_note => false).order('date DESC')
Note.where('created_at <= ?', 5.minutes.ago).where(:private_note => false).order('date DESC')
我一直都在从未离去 2024-12-13 20:47:27
Note.where('created_at < ?', Time.now.utc - 5.minutes)

对于像 Time.now 这样的东西要记住的一件事是,如果您最终将其放入作用域中,请使用 lambda 而不是直接将其传递到作用域,否则 Time.now 将是常量。

scope :older_than_5_minutes, lambda { order(...).where('...', Time.now.utc - 5.minutes) }
Note.where('created_at < ?', Time.now.utc - 5.minutes)

One thing to keep in mind with things like Time.now is that if you end up putting that into a scope, use a lambda instead of passing it directrly to the scope, otherwise Time.now will be constant.

scope :older_than_5_minutes, lambda { order(...).where('...', Time.now.utc - 5.minutes) }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文