Cannot convert nil to Array will_paginate

Hi guys, I have problems with will_paginate in rails 3, I have this in my constructor

@posts = Post.paginate(:all, :include => [:author, :categories], :conditions=>"status = 'published'", :order=>"blog_posts.created_at DESC", :per_page => 1, :page => params[:page]) 

and <% = will_paginate @ posts%> in my view, but I get that this error cannot continuously convert nil to an array.

Can anyone help me?

Thanks in advance

+6
ruby-on-rails
source share
3 answers

You need to install the will_paginate version 3.0.pre2 file to work with pages at the database level, and not in the array.

 gem 'will_paginate', '3.0.pre2' 
+11
source share

Try to do this:

 @posts = Post.includes(:author, :categories).where(:status => "published").order("blog_posts.created_at DESC").paginate(:per_page => 1, :page => params[:page]) 

This is the preferred Rails 3 path.

+8
source share

iain already answered this question, but I just wanted to add some data to support his claim that this command does not paginate the entire collection

 irb(main):005:0> Benchmark.bm do |x| irb(main):006:1* x.report {Vote.all.paginate(:per_page => 10, :page => 1)} irb(main):007:1> x.report {Vote.order("created_at DESC").paginate(:per_page => 10, :page => 1)} irb(main):008:1> end user system total real 210.490000 0.740000 211.230000 (214.754060) 0.000000 0.000000 0.000000 ( 0.429304) 

This was done in a database with 500,000 rows in the Vote (postgres) table, Rails 3.0.5

Therefore, I agree with him that scopes are added to the request before execution.

+2
source share

All Articles