Pi Horse
Pi Horse

Reputation: 2430

Pagination in Ruby when results fetched in a mySQL query

I fetch some rows in my Rails controller using the following query:

@main = connection.execute("SELECT code_ver, result FROM mastertest WHERE code_ver NOT LIKE '%DevBld%' AND date >= DATE_SUB( (CURDATE()) , INTERVAL 90 DAY) ORDER BY date DESC;")

Now in my index.html.erb (View) I display these rows in form of a table.

But I want to have next and previous buttons in my html page such that It should display only a few rows at a time. Lets assume a week's data at a time.

Upvotes: 0

Views: 394

Answers (2)

Alistair A. Israel
Alistair A. Israel

Reputation: 6567

For pagination, I'd go with Kaminari.

To fetch the 7th page of users, 20 per page:

User.page(7).per(20)

Better yet, to get nice First Prev 2 3 [4] 5 6 ... Next Last links in your views, all you need is:

<%= paginate @users %>

Upvotes: 2

John Beckwith Farmer
John Beckwith Farmer

Reputation: 39

I used the will_paginate gem that I learned about in the Rails tutorial. It works wonderfully.

Upvotes: 2

Related Questions