我使用以下代码来帮助查找当月的销售额。我的开发数据库是 SQLite,但我使用 Heroku 进行生产,它使用 PostgreSQL。所以我收到 strftime 函数不存在错误。我将如何翻译它,有没有办法翻译它,以便它在查询我的开发 SQLite 数据库时仍然有效?
sales = current_user.location.sales.find( :all,
:conditions => ["strftime('%Y', date) = '?'
AND strftime('%m', date) = ?",
Date.today.year,
Date.today.strftime('%m')],
:order => "date")
最佳答案
Postgres 不支持 strftime。您可以找到支持的日期/时间函数的列表 here .
sales = current_user.location.sales.find( :all,
:conditions => ["extract(year from date) = '?'
AND extract(month from date) = ?",
Date.today.year,
Date.today.strftime('%m')],
:order => "date")
关于ruby-on-rails - 帮助将 Rails Sqlite 查询转换为 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6903476/