The following result sets work well with will_paginate:
Members.all(:limit => 5).paginate(:page => params[:page])
Members.all(:conditions => ["member_no < 6"]).paginate(:page => params[:page])
Members.all.paginate(:page => params[:page])
The following does not:
Members.all(:conditions => ["member_no IN (?)", [1, 2, 3, 4, 5]]).paginate(:page => params[:page])
Why the second query does not work well with paginate? Thanks!
The #paginate is an instance method made available on Array and ActiveRecord::Base. You really should be doing it this way:
Member.paginate(:page => params[:page], :limit => 5)
Member.paginate(:conditions => ["member_no < ?", 6], :page => params[:page])
Member.paginate(:page => params[:page])
Member.paginate(:conditions => {:member_no => (1..5)}, :page => params[:page])
When you call #all then #paginate, what you're doing is asking for all members (all 1,000,000 of them), then discarding 99.999% of them, because you only want the first 10. This is very wasteful, to say the least.
Related
I am able to install this app on my windows 7 laptop https://github.com/cheezy/puppies
But now when I am trying to access it at localhost:3000 it is giving me error:
unsupported parameters: :order
I went to the file in this app and found this code:
app/controllers/agency_controller.rb
class AgencyController < ApplicationController
skip_before_filter :authorize
def index
#puppies = Puppy.paginate :page => params[:page], :order => 'name', :per_page => 4
end
end
While looking for a fix on this error I found a fix here https://github.com/mislav/will_paginate/issues/500
Where a comment by "mislav" says that "Active Record doesn't support :xyz formatting and it need to be written in User.where(conditions).order('title').per_page(per_page).page(page) format.
So, if it is a fix how to write #puppies = Puppy.paginate :page => params[:page], :order => 'name', :per_page => 4 in suggested format?
But if its not actual fix how to fix it?
I think that's because you are passing :order into the paginate method.
The correct query should be:
#puppies = Puppy.order(:name).paginate(page: params[:page], per_page: 4)
I am replacing will_paginate with Kaminari and large part of my application uses .paginate method with multiple conditions.
#posts = Post.published.paginate(:order => 'published_at desc, id',
:joins => :comments,
:conditions => conditions,
:group => "posts.id",
:per_page => 10,
:page => params[:page])
I am looking for quickest way and the most efficient way to replace such code with kaminari.
Rather then
#posts = Post.published.paginate.order('published_at desc').....page().per()
Is there a way in which we can paginate using with_exclusive_scope in Rails 2.3?
#albums = Album.paginate(:page => 1, :per_page => 12, :conditions => cond)
works fine.
Album.with_exclusive_scope {find(:all, :conditions => cond)}
works fine.
Can we combine with_exclusive_scope and paginate?
#albums = Album.find_allx(opts).paginate(:page => 1, :per_page => 12)
with_exclusive_scope is a protected method, so it has to be called from a class message. In this case the class method is find_allx()
If I click the next or previous link it doesn't go to the next or previous page.
All posts are on same page, but there are links next, previous at bottom.
In PostsController:
#posts = Post.paginate(:per_page => 15, :page => params[:page], :order => 'created_at DESC')
in posts/index:
<%= will_paginate #posts%>
Where is the problem with will_paginate?
You have to order before paginate,
so, change it to
#posts = Post.order('created_at DESC').paginate(:per_page => 15, :page => params[:page])
Not sure if this is causing your error, but ordering should be done outside of will_paginate.
#posts = Post.paginate(:per_page => 15, :page => params[:page]).order('created_at DESC')
This is how it should be done in Rails 3.
I've also had trouble setting the per_page parameter within the controller. You could try setting it in the model instead.
class Post
self.per_page = 10
end
Updating to will_paginate , '3.1.7' solved my issues
me again...
I need show 10 or 20 or 50 results number of results per page with a select options in my list of posts using will_paginate plugin
Can you help me please?
Thanks!
Looks like the OP also asked here: http://railsforum.com/viewtopic.php?id=33793 and got much better answers.
To adapt the best solution there, here's what I like:
(in the view)
<%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i),
:onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
(in the controller)
#per_page = params[:per_page] || Post.per_page || 20
#posts = Post.paginate( :per_page => #per_page, :page => params[:page])
To set a class wide Default
class Post < ActiveRecord::Base
def self.per_page
25
end
end
Or on a query by query basis use the per_page in your call
class Post <ActiveRecord::Base
def self.posts_by_paginate
paginate(:all, :per_page => 25, :conditions => ["published = ?", true])
end
end
Here is what I will do
Class UsersController < ApplicationController
def index
#users = User.paginate(:all, :page => params[:page], :per_page => params[:number_of_records])
end
end