Rails: activeadmin, undefined method `per' for #<ActiveRecord::Relation:0x4d15ee0>

ubuseral picture ubuseral · Feb 19, 2013 · Viewed 7.8k times · Source

I installed ActiveAdmin successfully:

My gemfile code:

source 'https://rubygems.org'

 gem 'rails', '3.2.10'

 # Bundle edge Rails instead:
 # gem 'rails', :git => 'git://github.com/rails/rails.git'

 gem 'sqlite3'


 # Gems used only for assets and not required
 # in production environments by default.
 group :assets do
   gem 'sass-rails',   '~> 3.2.3'
   gem 'coffee-rails', '~> 3.2.1'

   # See https://github.com/sstephenson/execjs#readme for more supported runtimes
   # gem 'therubyracer', :platforms => :ruby

   gem 'uglifier', '>= 1.0.3'
 end

 gem 'jquery-rails'

 gem 'twitter-bootstrap-rails'

 gem 'activeadmin'

  # gem "meta_search",    '>= 1.1.0.pre'
 gem "spud_photos"
 gem 'devise'

 gem 'cancan'
 gem 'rolify'

and i did this:

 bundle
 rails g active_admin:install
 rake db:migrate
 rails g active_admin:resource product

I linked some models to ActiveAdmin.

Error after clicking in the dashboard on the product link:

 undefined method `per' for #<ActiveRecord::Relation:0x4d15ee0>

Answer

mohamed-ibrahim picture mohamed-ibrahim · Feb 28, 2013

Active Admin need kaminari pagination If you want to use will paginate, you can make alias for will paginate functions to match kaminari one:

# 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

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

And this one worked for me.