问题
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