kaminari: Kaminari and will_paginate don't play nice together

There’s an issue when kaminari and will_paginate are loaded into the same project together.

It stems from where kaminari loads it’s Relation methods in scope :page whereas will_paginate defines a page method.

https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/active_record_model_extension.rb#L12-L17

https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/active_record.rb#L130-L139

This means that will_paginate runs roughshod over the scope and the Relation stuff never gets loaded. As you can see in the 2 patches they pretty much both do the same thing (set limit and offset).

I’ve kinda fixed the issue by

::ActiveRecord::Relation.send :include, Kaminari::ActiveRecordRelationMethods
::ActiveRecord::Relation.send :include, Kaminari::PageScopeMethods

in https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/railtie.rb#L18 but it’s nowhere near as elegant as the previous solution. forcing the inclusing of Kaminari::ActiveModel

About this issue

  • Original URL
  • State: open
  • Created 13 years ago
  • Reactions: 4
  • Comments: 21 (3 by maintainers)

Commits related to this issue

Most upvoted comments

One minor fix what I used to make it works

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value); self 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

I’ve heard you can fix this by doing something like:

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

Has anyone tested that this fixes the incompatibility? Waiting on feedback to close this ticket.

Has anyone come up with a solution? Iam using Refinery CMS which uses will_paginate and want to use active admin which uses kaminari.