views:

30

answers:

2

The situation will like this .... .... a user have many posts. But if I loop back all the posts, it will become very slow. So, I may look back the first ten (1-10). When the user click "next", it will get the (11-20). So, my question is, how can I implement it?? thank you.

<%=h @user.posts.inspect  %> <!--It can  show all the posts, but I want first ten only-->
+3  A: 

Most Rails developers use the will_paginate gem for this and with good reason because it's really very easy to use:

# In your controller
@posts = @user.posts.paginate, :page => params[:page]

# In your view template
<%= will_paginate @posts %>
John Topley
It seems great, I will get a try.
Tattat
I got a syntax error, so I use this: @user.posts.paginate :page => params[:page], but it still can't show any thing...I tried with <%=h @ posts.length %> <%= will_paginate @ posts %> It can show the length, but nothing other else
Tattat
If you are having problems with using will_paginate then please post it as a separate question with proper details.
John Topley
question ask, here: http://stackoverflow.com/questions/3230344/will-paginate-not-showing-on-the-webThank you very much.
Tattat
A: 

It depends on if you care if the user misses some posts or sees a few posts multiple times when the underlying list of posts changes. If you need to make sure that this doesn't happen you need to make a snapshot of the underlying list and then paginate over the snapshot list. I usually store the list snapshot in a db table and expire old entries with a daemon. If you use the approach you probably want to maximize the number of items in the list to something like 1000 posts so that the db entries do not become too large.

Peder