will_paginate: undefined local variable or method `per' for []:ActiveRecord::Relation

Hello, After adding will_paginate to my Gemfile I get this error:

gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git'

undefined local variable or method `per' for []:ActiveRecord::Relation

Rails.root: /home/slava/dev/projects/bitcoin-derivatives
Application Trace | Framework Trace | Full Trace

activerecord (3.1.0) lib/active_record/relation.rb:459:in `method_missing'
/home/slava/.rvm/gems/ruby-1.9.2-p290/bundler/gems/spree-037b9258f970/core/lib/spree/search/base.rb:15:in `retrieve_products'
/home/slava/.rvm/gems/ruby-1.9.2-p290/bundler/gems/spree-037b9258f970/core/app/controllers/home_controller.rb:7:in `index'

About this issue

  • Original URL
  • State: closed
  • Created 13 years ago
  • Comments: 31 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Actually this solution ain’t working anymore, if you still want to use will_paginate instead of kaminari you will have to modify your initializer to this :

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value) end
        def total_count() count end
      end
    end
    module CollectionMethods
      alias_method :num_pages, :total_pages
    end
  end
end

Thank you @andrewroth for the tip.

Just an FYI, this was broke for me as well when using activeadmin which requires Kaminari which happens to conflict with my existing version of will_paginate -v3.0.2. I found a similar issue here which described a workaround. What fixed it for me was to add an initializer to handle conflicting methods:

# config/initializers/will_paginate.rb
if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
      end
    end
  end
end

I’m using RefineryCMS with Spree Commerce. Here’s what my dependencies look like:

refinerycms (3.0.0)
  refinerycms-core (3.0.0)
    will_paginate (~> 3.0.2)

spree_core (3.0.1)
  kaminari (~> 0.15, >= 0.15.1)

I put the following in config/initializers/kaminari_config.rb to solve this problem:

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
        alias_method :prev_page, :previous_page
      end
    end
  end
end

If any of you have an extra issue with the a method known as ‘total count’, try this modified initializer:

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
        alias_method :total_count, :count
      end
    end
  end
end

Actually ctiveAdmin uses Kaminari for pagination We have used will_paginate in your app, due to that pagination get conflict, to remove this conflicts. Just create kaminari.rb file in your “config/initializers/kaminari.rb”

and type this code in it.

Kaminari.configure do |config| config.page_method_name = :per_page_kaminari end


Restart the server again and hope the issue get resolved.

I solved this problem by adding

ruby Spree::Product::ActiveRecord_Relation.include Kaminari::PageScopeMethods

to config/initializers/kaminari.rb

Spree replaced will_paginate with Kaminari and now is using its methods, one of which is per instead of per_page.

Since will_paginate and Kaminari are not compatible, you should remove it from your Gemfile.