Solvedactiveadmin undefined method `total_count' for #<ActiveRecord::Relation []>

We upgraded to Rails 5.1.4 and ActiveAdmin 1.1.0 and now an active_admin show page raises this exception. In my active-admin model file, I can comment out the line active_admin_comments and it works fine so I suspect the problem is in there. In the screenshot there is unfortunately no stack trace other than my model .rb file.

Expected behavior

No exception should be raised.

Actual behavior

Showing .../.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activeadmin-1.1.0/app/views/active_admin/resource/show.html.arb where line #2 raised:

undefined method `total_count' for #<ActiveRecord::Relation []>

screen shot 2017-11-09 at 15 58 57

How to reproduce

I make the simplest active-admin model file like so, and it crashes:

ActiveAdmin.register ClinicSource do
  show do
    active_admin_comments
  end
end
16 Answers

✔️Accepted Answer

I am a little late here, but for those who keep getting the same error, here is another solution:

# config/initializers/kaminari.rb
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

Other Answers:

@ghilead

Try adding an initializer as below:

/config/initializers/will_paginate.rb

module ActiveRecord
  class Relation
    alias_method :total_count, :count
  end
end 

I hope this will fix the issue.

More Issues: