![](/img/trans.png)
[英]Getting error function to_date(timestamp without time zone, unknown) does not exist
[英]Getting error “function week(timestamp without time zone) does not exist”
我正在关注此示例如何使用页面上的按钮(RoR)更改页面上显示的记录并设法使每日过滤器正常工作,但是当尝试为每周过滤器设置它时,我收到错误 PG::错误:错误:函数周(没有时区的时间戳)不存在
我认为 WEEK 函数应该像 DATE 函数一样自动工作,但事实并非如此。
模型
def self.popularToday
reorder('votes desc').find_with_reputation(:votes, :all, { :conditions => ["DATE(microposts.created_at) = DATE(NOW())"]})
end
def self.popularWeekly
reorder('votes desc').find_with_reputation(:votes, :all, { :conditions => ["WEEK(microposts.created_at) = WEEK(NOW())"]})
end
index.html.erb
<form action="" method="get">
<fieldset>
<button type="submit" name="show" value="daily">Today</button>
<button type="submit" name="show" value="weekly">This week</button>
</fieldset>
</form>
控制器
when "daily"
Kaminari.paginate_array(Micropost.popularToday).page(params[:page]).per(25)
when "weekly"
Kaminari.paginate_array(Micropost.popularWeekly).page(params[:page]).per(25)
你需要:
"SELECT EXTRACT(WEEK FROM TIMESTAMP microposts.created_at)"
这将根据 postgres 中创建的时间戳检索周数。
文档: http : //www.postgresql.org/docs/9.3/static/functions-datetime.html
具有我的时间戳但没有时区数据的表的名称称为 create_date。 我使用了 EXTRACT(WEEK FROM create_date) 并且它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.