Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
bitcoiners opened this Issue · 27 comments
@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

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

@mislav
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 mislav closed this
@marksands

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
@fu-media

thanks !

@snowmaker

@marksands - your initializer worked for me. thanks!

@cactis

@marksands, your initializer worked. thanks!

@jackquack

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

@marksands, thanks!

@kayloos

@jackquack, probs for that one.

@gsiener

@jackquack thanks much!

@zitooon

Thanks !

@zitooon

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-

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?

@dogweather

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

@chandresh

@marksands the solution works for me!

@sales

Hey, thanks @zitooon, it worked perfectly!

@yellowaj

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

@dennismonsewicz

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

@shamsulsham89

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

@joerails

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

@zehwoua

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

@linus-young

@marksands Bravo! Big thanks for your comment :D

@borfd

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

I solved this problem by adding

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

to config/initializers/kaminari.rb

@mplewis

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