will_paginate undefined method `total_pages'

Ruby on-RailsWill Paginate

Ruby on-Rails Problem Overview


What am I missing here? I am using the haml_scaffold generator and the pages work fine with will_paginate. When I start tinkering I end up with this 'total_pages' error and I'm not sure what I am doing that is causing it. Anyone have any insight? I'm using mislav-will_paginate 2.3.11.

mike@jauntyjackalope:~/project$ script/console
Loading development environment (Rails 2.3.4)
>> @locations = Location.find_all_by_city("springfield")
=> [#<Location id: 3, address: "123 Main St", city: "springfield", phone: "321-1234", business_id: 2>]
>> @locations.class
=> Array
>> @locations.collect{|loc| loc.business}
=> [#<Business id: 2, name: "A Bar", website: "www.abar.com", business_owner_id: 1, created_at: "2009-08-31 21:13:10", updated_at: "2009-08-31 21:13:10">]
>> @locations.class
=> Array
>> @locations.paginate(:page => 1, :per_page => 2)
=> [#<Location id: 3, address: "123 Main St", city: "springfield", phone: "321-1234", business_id: 2>]
>> helper.will_paginate(@locations)
NoMethodError: undefined method `total_pages' for #<Array:0xb6f83bc4>
 from /var/lib/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/view_helpers.rb:197:in `total_pages_for_collection'
 from /var/lib/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/view_helpers.rb:98:in `will_paginate'
 from (irb):7

>> y @locations
--- 
- !ruby/object:Location 
  attributes: 
    city: springfield
    business_id: "2"
    id: "3"
    phone: 321-1234
    address: 123 Main St
  attributes_cache: {}

  business: !ruby/object:Business 
    attributes: 
      name: A Bar
      updated_at: 2009-08-31 21:13:10
      website: www.abar.com
      id: "2"
      created_at: 2009-08-31 21:13:10
      business_owner_id: "1"
    attributes_cache: {}

=> nil
>> 

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

try to change

@locations.paginate(:page => 1, :per_page => 2)

to

@locations = @locations.paginate(:page => 1, :per_page => 2)

hope this helps

Solution 2 - Ruby on-Rails

include

require 'will_paginate/array' 

before loading that code will solve your problem.

Solution 3 - Ruby on-Rails

If anyone else is having this problem. In my case I did not call the pagination in my controller method last.

Example Before:

@foo = Foo.paginate(:page => params[:page], :per_page => 15)
@foo = Foo.do something

Example After: I called the pagination last within my method since rails reads from top to bottom and it seemed to fix my error.

@foo = Foo.do something
@foo = Foo.paginate(:page => params[:page], :per_page => 15)

Solution 4 - Ruby on-Rails

@locations = Location.paginate(:all, :conditions => 'city = springfield')

@locations must be an object

in your example @locations is an Array

array cant have total_pages method

Solution 5 - Ruby on-Rails

I was encountering a similar a error undefined method 'total_pages' for ActiveRecord_AssociationRelation.

It turns out that renaming my variable solved the issue.

Here was my code:

@reviews = @user.reviews.paginate(page: params[:page], per_page: 5).order('created_at DESC')

I changed it to this:

@user_reviews = @user.reviews.paginate(page: params[:page], per_page: 5).order('created_at DESC')

And it solved the issue. It's very bizarre but it there may have been some kind of conflict.

Since I spent a few hours researching, I thought it might help someone one day as well!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmikewilliamsonView Question on Stackoverflow
Solution 1 - Ruby on-RailsJirapongView Answer on Stackoverflow
Solution 2 - Ruby on-RailsNeeraj KumarView Answer on Stackoverflow
Solution 3 - Ruby on-RailscoletrainView Answer on Stackoverflow
Solution 4 - Ruby on-RailsbatmanView Answer on Stackoverflow
Solution 5 - Ruby on-RailsNZisKoolView Answer on Stackoverflow