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

Comments

Projects
None yet
@bitcoiners

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

This comment has been minimized.

Show comment
Hide comment
@coryschires

coryschires Sep 5, 2011

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

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

@mislav

This comment has been minimized.

Show comment
Hide comment
@mislav

mislav Sep 6, 2011

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.

Owner

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

This comment has been minimized.

Show comment
Hide comment
@marksands

marksands 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

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

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Oct 16, 2011

thanks !

ghost commented Oct 16, 2011

thanks !

@snowmaker

This comment has been minimized.

Show comment
Hide comment
@snowmaker

snowmaker Oct 25, 2011

@marksands - your initializer worked for me. thanks!

@marksands - your initializer worked for me. thanks!

@cactis

This comment has been minimized.

Show comment
Hide comment
@cactis

cactis Oct 26, 2011

@marksands, your initializer worked. thanks!

cactis commented Oct 26, 2011

@marksands, your initializer worked. thanks!

@hrdwdmrbl

This comment has been minimized.

Show comment
Hide comment
@hrdwdmrbl

hrdwdmrbl 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

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

This comment has been minimized.

Show comment
Hide comment

blankyao commented Dec 7, 2011

@marksands, thanks!

@kayloos

This comment has been minimized.

Show comment
Hide comment
@kayloos

kayloos Dec 20, 2011

@jackquack, probs for that one.

kayloos commented Dec 20, 2011

@jackquack, probs for that one.

@gsiener

This comment has been minimized.

Show comment
Hide comment

gsiener commented Dec 26, 2011

@jackquack thanks much!

@zitooon

This comment has been minimized.

Show comment
Hide comment

zitooon commented Mar 13, 2012

Thanks !

@zitooon

This comment has been minimized.

Show comment
Hide comment
@zitooon

zitooon 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.

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-

This comment has been minimized.

Show comment
Hide comment
@bruno-

bruno- Mar 15, 2012

zitooon - thanks, this worked!

bruno- commented Mar 15, 2012

zitooon - thanks, this worked!

@Terfender

This comment has been minimized.

Show comment
Hide comment
@Terfender

Terfender 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?

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

@dogweather

This comment has been minimized.

Show comment
Hide comment

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

@chandresh

This comment has been minimized.

Show comment
Hide comment
@chandresh

chandresh Oct 3, 2012

Contributor

@marksands the solution works for me!

Contributor

chandresh commented Oct 3, 2012

@marksands the solution works for me!

@sales

This comment has been minimized.

Show comment
Hide comment
@sales

sales Nov 1, 2012

Hey, thanks @zitooon, it worked perfectly!

sales commented Nov 1, 2012

Hey, thanks @zitooon, it worked perfectly!

@yellowaj

This comment has been minimized.

Show comment
Hide comment
@yellowaj

yellowaj Jan 9, 2013

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

yellowaj commented Jan 9, 2013

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

@dennismonsewicz

This comment has been minimized.

Show comment
Hide comment
@dennismonsewicz

dennismonsewicz Mar 7, 2013

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

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

@shamsulsham89

This comment has been minimized.

Show comment
Hide comment
@shamsulsham89

shamsulsham89 Mar 11, 2013

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

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

@joerails

This comment has been minimized.

Show comment
Hide comment
@joerails

joerails May 30, 2013

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

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

@zehwoua

This comment has been minimized.

Show comment
Hide comment
@zehwoua

zehwoua Sep 2, 2013

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

zehwoua commented Sep 2, 2013

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

@linus-young

This comment has been minimized.

Show comment
Hide comment
@linus-young

linus-young Jan 18, 2014

@marksands Bravo! Big thanks for your comment :D

@marksands Bravo! Big thanks for your comment :D

jarray52 pushed a commit to jarray52/forem that referenced this issue Apr 9, 2014

@borfd

This comment has been minimized.

Show comment
Hide comment
@borfd

borfd 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

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

This comment has been minimized.

Show comment
Hide comment
@vernondegoede

vernondegoede Dec 16, 2014

I solved this problem by adding

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

to config/initializers/kaminari.rb

I solved this problem by adding

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

to config/initializers/kaminari.rb

@mplewis

This comment has been minimized.

Show comment
Hide comment
@mplewis

mplewis 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

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

This comment has been minimized.

Show comment
Hide comment
@SanjayYadav90

SanjayYadav90 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.

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

This comment has been minimized.

Show comment
Hide comment

quocnv commented Aug 11, 2015

Thanks

@hoangvq288

This comment has been minimized.

Show comment
Hide comment
@hoangvq288

hoangvq288 Dec 14, 2015

Thanks. It worked for me

Thanks. It worked for me

@artemdev

This comment has been minimized.

Show comment
Hide comment

artemdev commented Jan 7, 2017

thanks!

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