user1903663
user1903663

Reputation: 1725

ruby sinatra, postgres paging results

I want to page a dataset.

The Ruby code is:

get '/candy' do
    @pagedata = { :cur_page => 1, :post_count => 0, :per_page => 3 }
    # set the current page to show
    @pagedata[:cur_page] = params[:page].to_i unless params[:page].nil?
    @cands = DB[:candidates] 
    #@cands = Post.by_created_at
    # set post_count
    @pagedata[:post_count] = DB[:candidates].count(:id)
    @cands = @cands.paginate(:page => @pagedata[:cur_page], :per_page => @pagedata[:per_page]) unless @cands.empty?
        erb :candy
    end

What's the problem? All help gratefully received!

The line:

@cands = @cands.paginate(:page => @pagedata[:cur_page], :per_page => @pagedata[:per_page]) unless @cands.empty?

Produces the error:

undefined method `paginate' for Sequel::Postgres::Dataset: "SELECT * FROM "candidates"

Upvotes: 1

Views: 302

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12139

You need to add DB.extension(:pagination) after creating your DB object.

Upvotes: 3

Related Questions