Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bitcoiners opened this issue Sep 5, 2011 · 31 comments
Closed
Labels

Comments

@bitcoiners
Copy link

@bitcoiners bitcoiners commented Sep 5, 2011

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'
@coryschires
Copy link

@coryschires coryschires commented Sep 5, 2011

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

@mislav
Copy link
Owner

@mislav mislav commented Sep 6, 2011

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 mislav closed this Sep 6, 2011
@marksands
Copy link

@marksands marksands commented Oct 12, 2011

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
@ghost
Copy link

@ghost ghost commented Oct 16, 2011

thanks !

@snowmaker
Copy link

@snowmaker snowmaker commented Oct 25, 2011

@marksands - your initializer worked for me. thanks!

@cactis
Copy link

@cactis cactis commented Oct 26, 2011

@marksands, your initializer worked. thanks!

@hrdwdmrbl
Copy link

@hrdwdmrbl hrdwdmrbl commented Nov 3, 2011

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
@blankyao
Copy link

@blankyao blankyao commented Dec 7, 2011

@marksands, thanks!

@kayloos
Copy link

@kayloos kayloos commented Dec 20, 2011

@jackquack, probs for that one.

@gsiener
Copy link

@gsiener gsiener commented Dec 26, 2011

@jackquack thanks much!

@zitooon
Copy link

@zitooon zitooon commented Mar 13, 2012

Thanks !

@zitooon
Copy link

@zitooon zitooon commented Mar 13, 2012

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-
Copy link

@bruno- bruno- commented Mar 15, 2012

zitooon - thanks, this worked!

@Terfender
Copy link

@Terfender Terfender commented Apr 19, 2012

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

@dogweather
Copy link

@dogweather dogweather commented Apr 26, 2012

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

@chandresh
Copy link
Contributor

@chandresh chandresh commented Oct 3, 2012

@marksands the solution works for me!

@sales
Copy link

@sales sales commented Nov 1, 2012

Hey, thanks @zitooon, it worked perfectly!

@yellowaj
Copy link

@yellowaj yellowaj commented Jan 9, 2013

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

@dennismonsewicz
Copy link

@dennismonsewicz dennismonsewicz commented Mar 7, 2013

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

@shamsulsham89
Copy link

@shamsulsham89 shamsulsham89 commented Mar 11, 2013

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

@joerails
Copy link

@joerails joerails commented May 30, 2013

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

@zehwoua
Copy link

@zehwoua zehwoua commented Sep 2, 2013

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

@linus-young
Copy link

@linus-young linus-young commented Jan 18, 2014

@marksands Bravo! Big thanks for your comment :D

jarray52 pushed a commit to jarray52/forem that referenced this issue Apr 9, 2014
@borfd
Copy link

@borfd borfd commented Apr 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
@vernondegoede
Copy link

@vernondegoede vernondegoede commented Dec 16, 2014

I solved this problem by adding

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

to config/initializers/kaminari.rb

@mplewis
Copy link

@mplewis mplewis commented Jun 12, 2015

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
@SanjayYadav90
Copy link

@SanjayYadav90 SanjayYadav90 commented Jul 30, 2015

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.

@quocnv
Copy link

@quocnv quocnv commented Aug 11, 2015

Thanks

@hoangvq288
Copy link

@hoangvq288 hoangvq288 commented Dec 14, 2015

Thanks. It worked for me

@artemdev
Copy link

@artemdev artemdev commented Jan 7, 2017

thanks!

mlandauer added a commit to openaustralia/planningalerts that referenced this issue Mar 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.