undefined method `total_pages' for #<Array:0xb3da36f4> #195

railsfactory-dhibika opened this Issue Nov 7, 2011 · 7 comments


None yet

4 participants


In controller i have added the code

@partner_deals=(Partner.find_by_id(params[:id]).deals.reject{|x| x.deal_date>Date.today}).paginate(:page => params[:page],:per_page => 2)   

i added require 'will_paginate/array' in controller

In view

<%= will_paginate @partner_deals %>

but the error "undefined method `total_pages' for #Array:0xb3da36f4" shows


That's odd; if you required will_paginate/array then you should be able to paginate static arrays. In fact, you don't get an exception for paginate call in the controller, just in the view. Can you check the class of the @partner_deals object?

BTW, you can move your filtering logic to the database and then you can paginate in the database and not require will_paginate/array (I'm assuming you're using Rails 3):

## model
class Deal
  scope :relevant, where(['deal_date <= ?', Date.today])

## controller
@partner = Partner.find(params[:id])
@partner_deals = @partner.deals.relevant.per_page(2).page(params[:page])

Closing due to lack of information

@mislav mislav closed this Dec 20, 2011

I have this error as well.


Perhaps I can provide more information:

(I'm using posts here because that is what you are using in your wiki/readme. Due to certain requirements of my application I need the end result to return an array of activerecord objects, and not an activerecord object itself.)

In config/initializers/array_paginate.rb

require 'will_paginate/array'

In my controller:

def search
  @results = []
  @posts = Posts.where(a: b, c: d)
  @posts.each do | post |
    if some_search_logic  
      @results >> post
  return @results.paginate(page: params[:page], per_page: 15)

in the view:


 <%= will_paginate @results %>

Going to the page raises this error:

undefined method 'total_pages\' for #<Array:0x000001128186a8>

Please let me know if there is any more information I can provide.


I've reproduced the issue here: https://github.com/tomkadwill/will_paginate_example/tree/array_issue_195

Will investigate further.


So I actually got this fixed. I had to import it within the controller and not in initializers.


Cool, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment