will_paginate 可以按天排序吗
场景:我有一个包含数百张照片的图片表。我目前正在使用“will_paginate”对每页 100 张照片进行分页。我想继续使用“will_paginate”但我希望分页由日期驱动。
我已经使用sort_by
尝试了以下操作,但我认为这不是在职的。
@pics = Picture.paginate(:page => params[:page]).sort_by(&:created_at)
最终目标:
首页仅显示今天的图片。
现在,如果我点击第 2 页,它只会显示昨天的照片。
现在,如果我点击第 3 页,它会显示两天前拍摄的照片。
我想继续使用 will_paginate
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用按日期按组/范围进行分页。您需要自己做这件事。不过这并不是非常困难,你可以这样做:
# pictures_controller.rb
# pictures/index.html.haml
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
# pictures/index.html.haml