自从我的开发环境默认设置为 sqllite3 以来,我在将 Rails 代码部署到 Heroku 时遇到了一些问题。这是以下日志快照:
ActiveRecord::StatementInvalid (PG::Error: ERROR: syntax error at or near ":"
2012-05-25T14:11:55+00:00 app[web.1]:
2012-05-25T14:11:55+00:00 app[web.1]: LINE 1: ...LECT "records".* FROM "records" WHERE (TIMESTAMP(:order) >=...
2012-05-25T14:11:55+00:00 app[web.1]: : SELECT "records".* FROM "records" WHERE (TIMESTAMP(:order) >= '2011-05-25 00:00:00' and TIMESTAMP(:order) <= '2012-05-25 23:59:59') ORDER BY created_at):
2012-05-25T14:11:55+00:00 app[web.1]: app/controllers/records_controller.rb:20:in `index'
我的控制器文件中的代码片段:
opts = {:order => "created_at"}
opts[:conditions] = (@start_date.nil? ? "" : "TIMESTAMP(created_at::text) >= '#{@start_date.to_s(:db)}'")
opts[:conditions] += ((@start_date.nil? || @end_date.nil?) ? "" : " and ")
opts[:conditions] += (@end_date.nil? ? "" : "TIMESTAMP(created_at::text) <= '#{@end_date.to_s(:db)}'")
最初,我有一个“DATETIME”,你有 TIMESTAMP,所以我切换了它,但仍然遇到问题。此代码的上下文是我正在使用以下格式在日期之间运行查询:2011-05-25 00:00:00