Skip to content
This repository

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

Closed
slavix opened this Issue September 04, 2011 · 25 comments
Slava

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'
Cory Schires

Having this same issue while using Rails 3.1 and ActiveRecord while trying to load active_admin views.

Mislav Marohnić
Owner

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.

Mislav Marohnić mislav closed this September 06, 2011
Mark Sands

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
Huan Son

thanks !

Jared Friedman

@marksands - your initializer worked for me. thanks!

chitsung.lin

@marksands, your initializer worked. thanks!

Marc

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
Claus Skou Nielsen

@jackquack, probs for that one.

Graham Siener

@jackquack thanks much!

Olivier de Robert

Thanks !

Olivier de Robert

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.

Bruno Sutic

zitooon - thanks, this worked!

Terfender

I don't have will_paginate.rb in config/initializers/
I am using the latest version which is 3.0.3
Any idea?

Robb Shecter

It looks like this is the only currently working solution: https://github.com/gregbell/active_admin/wiki/How-to-work-with-will_paginate

Chandresh Pant

@marksands the solution works for me!

Matheus Sales

Hey, thanks @zitooon, it worked perfectly!

Adam Jacox

@jackquack that solved the extra issue for me. Thanks!

Dennis Monsewicz

@jackquack thanks so much for this solution!! Saved my ass on a big project!

Shamsul Haque

@zitooon
Thanks it worked for active admin ruby 1.9.2 rails 3.2.11

Joe weru

@zitooon Thanks ...yes it did work.
God bless all

zehwoua

Great thank you for will_paginate tip. Sort me out!!

whyisyoung

@marksands Bravo! Big thanks for your comment :D

Boris Filipov
borfd commented April 13, 2014

I had to add two additional methods to make this work

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value) end
        def total_count() count end
        def first_page?() self == first end
        def last_page?() self == last end
      end
    end
    module CollectionMethods
      alias_method :num_pages, :total_pages
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.