ben*_*ben 2 time activerecord ruby-on-rails ruby-on-rails-3
编写返回至少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)
Run Code Online (Sandbox Code Playgroud)
得到这些错误:
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'
Run Code Online (Sandbox Code Playgroud)
一个示例created_at值是 2011-09-28 01:00:01 UTC
Note.where('created_at <= ?', 5.minutes.ago).where(:private_note => false).order('date DESC')
Run Code Online (Sandbox Code Playgroud)