【发布时间】:2014-04-05 03:32:42
【问题描述】:
我按照这个例子How do I change the records being displayed on the page with buttons on the page (RoR) 并设法让每日过滤器工作,但是当尝试为每周过滤器设置它时,我得到了错误 PG::Error: ERROR: function week(timestamp without time zone) 不存在
我认为 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)
【问题讨论】:
标签: sql date ruby-on-rails-4