FAQ

lvxn0va edited this page Jan 16, 2013 · 4 revisions

I'm seeing undefined method 'page'.

Search your project for #page_method_name to see if you ever configured a different name. If you were using will_paginate and ActiveAdmin at the same time before, you probably did it. In that case, try to use #kaminari_page instead of #page. Or, remove away the #page_method_name config from your app then things start working as you expect. Or another alternative is to place require "kaminari" at the top of your application.rb before the railties requires.

How can I know which page a record is on?

If you are using ActiveRecord, you can get it by doing like this:

class Foo < AR::Base

  def page_num(options = {})
    column = options[:by] || :id
    order  = options[:order] || :asc
    per    = options[:per] || self.class.default_per_page

    operator = (order == :asc ? "<=" : ">=")
    (self.class.where("#{column} #{operator} ?", read_attribute(column)).order("#{column} #{order}").count.to_f / per).ceil
  end

end

foo = Foo.find(params[:id])

foo.page_num
foo.page_num(:by => :other_column, :order => :asc, :per => 10)
# => 1, 2 or whatever numbers...

Basically, what it does is:

  • get a count number of all the results that are listed before the given record/value.
  • Divide the count by default_per_page or per number given as an option.
  • Ceil the number to get a proper page number that the record/value is on.

So this can be done with array/ORMs.