开发者

Rails 3 - How to handle pagination according to time frame, not the number of posts?

I would like to write a pagination functionality for my Rails 3 application that will break the posts in forum to pages. The division to pages is done according to a time frame (configurable by user), say 7 days, i.e. the first page shows posts from the last week, the second page shows the posts from the week before that, and so on.

Moreover, I would like to display some indication for users about the number of posts on each page. For example, I could set the title attribute of the link to the second page to "74 posts", or to change the background color according to the number of posts.

In other words, I need to know the number of posts on each page when rendering a page. Initially, I thought to store the number of posts on each page in a database, and update this number when a new message is added,开发者_C百科 or a message is deleted. But, since the time frame is configurable (it can be 7 days, 3 days, 1 month, or even 1 day), I'm not sure how to manage this is the database. Also, I don't want to load all posts from the database because the loading time will be very slow.

Please advise.


Add two params to your index action for date_from and date_to. Use these to select the records from the database and return them to the index.

See ActiveRecord range conditions for more info.

As for counting the links in these ranges simply call count on them to return the number of items.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜