will_paginate can it order by day

落花浮王杯 提交于 2019-12-01 12:47:07

问题


SCENARIO: I have a Pictures table that contains hundreds of photos. I'm currently using 'will_paginate' to paginate 100 photos per page. I would like to continue using 'will_paginate' BUT I want the pagination to be driven by the day.

I have tried the following by using sort_by but I don't think it's working.

@pics = Picture.paginate(:page => params[:page]).sort_by(&:created_at)

END GOAL:
Home page shows me today's pictures only.
Now if I click on page 2 it shows me yesterdays pictures only.
Now if I click on page 3 it shows me pictures taken from two days ago.

I would like to continue using will_paginate


回答1:


You cannot use will paginate by group/scope by date. You'll need to do this yourself. It's not terribly difficult though, you could do something like this:

# pictures_controller.rb

def index
  @date             = Date.parse(params[:date]) rescue Date.today
  @pictures         = Picture.where(:created_at => @date.at_midnight..@date.next_day.at_midnight)

  @total_pictures   = Picture.count
  @current_pictures = @pictures.size
end

# pictures/index.html.haml

- @pictures.each do |picture|
  = #....

= "Showing #{@current_pictures} pictures for #{@date}."
= "There are a total of #{@total_pictures} pictures."

= link_to 'View Previous day photos', pictures_url(:date => @date.prev_day)
- if @date.past?
  = link_to 'View Next day photos', pictures_url(:date => @date.next_day)


来源:https://stackoverflow.com/questions/8691211/will-paginate-can-it-order-by-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!