Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Postgres中strftime的等效值_Ruby On Rails_Postgresql_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Postgres中strftime的等效值

Ruby on rails Postgres中strftime的等效值,ruby-on-rails,postgresql,ruby-on-rails-4,Ruby On Rails,Postgresql,Ruby On Rails 4,在将SQLite数据库迁移到开发服务器上的Postgres之后,我得到了 PG::UndefinedFunction: ERROR: function strftime(unknown, date) does not exist 是否有一个功能我可以用来保持我的整个应用程序的功能 编辑:尝试使用字符,但失败惨重。特别是,我不明白如何在诸如 def self.query_by_year_month(y, m) where("strftime('%Y', date) = ? and strf

在将SQLite数据库迁移到开发服务器上的Postgres之后,我得到了

PG::UndefinedFunction: ERROR:  function strftime(unknown, date) does not exist
是否有一个功能我可以用来保持我的整个应用程序的功能

编辑:尝试使用字符,但失败惨重。特别是,我不明白如何在诸如

def self.query_by_year_month(y, m)
  where("strftime('%Y', date) = ? and strftime('%m', date) = ?", y, m)
end
谢谢大家!

你最好的选择可能是


另一种方法是将strftime()的行为实现为用户定义的函数。(当然名为“strftime”)

您可能想使用
date\u part()。请参阅此处的文档

现在似乎是开始学习SQL的好时机!使用以下代码使我的方法工作:

  def self.query_by_year_month(y, m)
    where("extract(year from date) = ? and extract(month from date) = ?", y, m)
  end

可以在视图层中使用to_char吗?一方面,您可能会从WHERE子句中获得一个顺序扫描。另一方面,您可以学习更多的SQL!