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-->Answer1:
Most Rails developers use the <a href="http://rubygems.org/gems/will_paginate" rel="nofollow">will_paginate</a> 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 %>Answer2:
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.