Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

activeadmin

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>
like image 591
ubuseral Avatar asked Feb 19 '13 13:02

ubuseral


2 Answers

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.

like image 119
mohamed-ibrahim Avatar answered Oct 19 '22 19:10

mohamed-ibrahim


This one helped me:

 if defined?(WillPaginate)
   ActiveSupport.on_load :active_record do
     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
 end
like image 23
drKreso Avatar answered Oct 19 '22 20:10

drKreso