60376

what is the common practice on limit the result in RoR?

Question:

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.

Recommend

  • Rake aborting with error
  • Why Rails app showing database information at the bottom?
  • Rails WillPaginate::Collection not paginating Array
  • Heroku installing sqlite3 even though it is not a dependecy
  • What part of this Solr-Sunspot setup am I missing?
  • how to pass url variable to next pages in php pagination
  • Michael Hartl RoR tutorial chapter 12 exercise #2
  • Read stdin in chunks in Bash pipe
  • Google Places API - Get more than 10 photos from Details Response?
  • Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often
  • Connecting Google Cloud SQL with Wordpress on Google Compute Engine
  • How to make SASS put relative paths in its output
  • Heroku push rejected - Hartl's Rails 3.2 tutorial
  • Error while using PDF Kit
  • Two Tables Serving as one Model in Rails
  • How to override value that appears in a dropdown in the rails_admin gem
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Ember.js model to be organised as a tree structure
  • Loading .coffee files via a view in Rails
  • Debug.DrawLine not showing in the GameView
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Could not find rake using whenever rails
  • Yii2: Config params vs. const/define
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Incrementing object id automatically JS constructor (static method and variable)
  • Sending data from AppleScript to FileMaker records
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Do create extension work in single-user mode in postgres?
  • Web-crawler for facebook in python
  • bootstrap to use multiple ng-app
  • A cron job substitute?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Binding checkboxes to object values in AngularJs
  • How to load view controller without button in storyboard?