Topic: Paginating with couchrest_model [SOLVED]


In my rails application, I am using couchrest_model to connect to a couchDB database.

I have a model like this:

class Author < CouchRest::Model::Base

  property :id, Integer
  property :title, String
  property :first_name, String
  property :last_name, String

  # view to get only the documents which starts with "authors" - Table Authors
  view_by :id,
          :map => "function(doc) {
            if(doc._id.indexOf('authors') == 0) {
              emit(null, doc);


In the controller the function is simple:

def list
   @authors = Author.by_id()

And in the view I iterate the @authors to show the results.

I would like to implement pagination but I don't know how. I already implemented pagination before, when I was using a PostgreSQL database and active records (will_paginate), but now, I am stuck with this.

Anyone can help me with a simple example?

Last edited by pjmorce (2012-06-12 10:23:40)

Re: Paginating with couchrest_model [SOLVED]

I solved my problem.

.... In the controller...

def list
    auths = BdsDataAuthor.by_id()
    page = params[:page]
    if page.nil?
      page = 1
    @authors = WillPaginate::Collection.create(page, BdsDataAuthorsHelper::PER_PAGES) do |pager|
      start = (page.to_i - 1) * BdsDataAuthorsHelper::PER_PAGES # assuming current_page is 1 based.
      pager.replace(auths[start, BdsDataAuthorsHelper::PER_PAGES])
      pager.total_entries = auths.count

... and in the view (in the end of the page view)...

<%= will_paginate @authors %>

(of course, I've installed will_paginate first...)

Last edited by pjmorce (2012-06-12 10:15:52)