如何在 PGSQL 中按日期搜索?
我收到此错误:
ActionView::Template::Error (PGError: ERROR: 操作符不存在: 没有时区的时间戳 ~~ 未知 LINE 1: ... "articles" WHERE ("articles"."created_at" LIKE ' 2010...
我有一个存档控制器,可以按流派和年、月、日动态显示文章,无论这些字段在 mysqlite 中可用,在 mysqlite 中,我有这个索引操作:
def index
filter_title
@articles = Article.where(:created_at.matches % date_builder, :genre.matches % genre_builder).order("created_at DESC")
respond_to do |format|
format.json { render :json => @articles }
format.xml { render :xml => @articles }
format.html
end
end
以及这个 date_builder 函数
def date_builder
@date = ""
@date += params[:year] if !(params[:year].nil?)
@date += "-" + params[:month] if !(params[:month].nil?)
@date += "-" + params[:day] if !(params[:day].nil?)
@date += "%"
@date
end
。使用metawhere 查找与我提供的字符串部分相匹配的日期。这在Sqlite 中完美运行。我已将我的应用程序迁移到heroku 和PGSQL,但它不允许我执行此操作,谢谢。 !
I am getting this error:
ActionView::Template::Error (PGError: ERROR: operator does not exist: timestamp without time zone ~~ unknown LINE 1: ... "articles" WHERE ("articles"."created_at" LIKE '2010...
I have an archive controller where I can dynamically display articles by genre and year, month and day, with whichever of those fields are available in the url. In mysqlite, I had this index action:
def index
filter_title
@articles = Article.where(:created_at.matches % date_builder, :genre.matches % genre_builder).order("created_at DESC")
respond_to do |format|
format.json { render :json => @articles }
format.xml { render :xml => @articles }
format.html
end
end
And this date_builder function
def date_builder
@date = ""
@date += params[:year] if !(params[:year].nil?)
@date += "-" + params[:month] if !(params[:month].nil?)
@date += "-" + params[:day] if !(params[:day].nil?)
@date += "%"
@date
end
that would use metawhere to find dates that matched the part of the string that I supplied. This worked perfectly in Sqlite. I have migrated my application onto heroku and PGSQL, and it doesn't allow me to do this. What is the solution? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 postgres 中,时间戳不像 sqllite 那样存储为字符串:
使用
>=
代替怎么样?In postgres, a timestamp is not stored as a string like in sqllite:
how about using
>=
instead?虽然我喜欢@JackPDouglas的答案,但我想尝试尽可能地将sql代码保留在我的项目之外,所以我最终这样做了:
以及日期的这个方法:
While I liked @JackPDouglas answer, I wanted to try and keep sql code out of my project as much as possible, so i ended up doing this:
and this method for the date: