activeadmin: undefined method `total_count' for #

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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

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

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

@AaronGaim Are you also using will_paginate? If you are, have you tried the fix mentioned here?

@winstonwolff @ghilead as for the issues you’re having. I think they’ll be resolved by monkey patching will_paginate to respond to the kaminari interface that AA expects, however I think we should close this issue (since it’s not actually a bug with AA).

If one of you wants to open a discussion-based issue around adding the features from will_paginate that you’re using into AA (or other similar feature requests), feel free to do so. For now though @varyonic I think this issue should be closed.

@winstonwolff Thanks for the report. I think we’ll need more information though. Using the environment you described (RoR 5.1.4 and AA 1.1.0) I made a sample app with a show page exactly like what you said should reproduce the problem:

ActiveAdmin.register AdminUser do
  show do
    active_admin_comments
  end
end

and I don’t get any errors: image

Can you include the source for the ClincSource model as well as your Gemfile?

Hi Will—

I found this with a suspicious comment. I’ll investigate this on Monday.

Thank you for your time investigating this. Hopefully I can figure out the conflict.

config/initializers/kaminari.rb

# Needed for active admin and will paginate to co-exist
Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari
end

```