# Will\_paginate question

**URL:** <https://www.ruby-forum.com/t/will-paginate-question/183511>\
**Category:** Rails\
**Created:** [February 15, 2010, 8:55pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511 "2010-02-15T20:55:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 8:55pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/1 "2010-02-15T20:55:40Z")

</div>

I have a Persons table created with scaffold. I was trying to flick  
between records by clicking on Previous and Next links. I was  
struggling to understand will\_paginate, and finally came up with  
following solution, and I’m wondering if it could be done better.  
Would it be better to have one query to a database returning previous  
current and next record instead having two?

@person\_previous = Person.paginate :all, :page =\> params[:page],  
:order =\> ‘id DESC’, :per\_page =\> 1, :conditions =\> “id \<  
#{params[:id]}”  
@person\_next = Person.paginate :all, :page =\> params[:page],  
:order =\> ‘id ASC’, :per\_page =\> 1, :conditions =\> “id \>  
#{params[:id]}”

\<% @person\_previous.each do |el|%\>  
\<%= link\_to ‘Previous’, el %\>  
\<% end %\>

\<% @person\_next.each do |el|%\>  
\<%= link\_to ‘Next’, el %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 9:44pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/2 "2010-02-15T21:44:15Z")

</div>

Rewriting your example a little, I think I would do it like this:

# controller

@person = Person.find(params[:id])  
@person\_previous = Person.first, :order =\> ‘id DESC’, :conditions =\>  
[“id \<  
?”, @person.id]  
@person\_next = Person.first, :order =\> ‘id ASC’, :conditions =\> [“id \>  
?”, @  
person.id]

# view

\<%= link\_to ‘Previous’, @person\_previous %\>  
\<%= link\_to ‘Next’, @person\_next %\>

- removed the paging
- secured the conditions against sql injection

– Lasse

2010/2/15 Bigos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/3 "2010-02-15T22:29:55Z")

</div>

Hi Bigos,

You aren’t using the view helper will\_paginate gives you. It’s as  
simple as:

@people = Person.paginate :page =\> params[:page]

And in your view:

\<%= will\_paginate @posts %\>

This will automatically generate the ‘Previous’ and ‘Next’ links. For  
further questions there is a dedicated will\_paginate group at  
[http://groups.google.com/group/will\_paginate](http://groups.google.com/group/will_paginate) – they’ll be able to  
help you better!

– Evan

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 10:27pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/4 "2010-02-15T22:27:09Z")

</div>

Jacek P. wrote:

> I have a Persons table created with scaffold. I was trying to flick  
> between records by clicking on Previous and Next links. I was  
> struggling to understand will\_paginate, and finally came up with  
> following solution, and I’m wondering if it could be done better.  
> Would it be better to have one query to a database returning previous  
> current and next record instead having two?
> 
> @person\_previous = Person.paginate :all, :page =\> params[:page],  
> :order =\> ‘id DESC’, :per\_page =\> 1, :conditions =\> “id \<  
> #{params[:id]}”  
> @person\_next = Person.paginate :all, :page =\> params[:page],  
> :order =\> ‘id ASC’, :per\_page =\> 1, :conditions =\> “id \>  
> #{params[:id]}”
> 
> \<% @person\_previous.each do |el|%\>  
> \<%= link\_to ‘Previous’, el %\>  
> \<% end %\>
> 
> \<% @person\_next.each do |el|%\>  
> \<%= link\_to ‘Next’, el %\>  
> \<% end %\>

First, yeah, never directly interpolate query parameters (or any user  
input) into your SQL queries. Second, use the paginate method to only  
fetch the records relevant for the current page, as the will\_paginate  
view helper takes care of the rest. Did you look at the documentation  
([gitrdoc.com](http://gitrdoc.com/mislav/will_paginate/tree/master/)) for the  
will\_paginate method? It’s designed to generate the Previous, Next, and  
anything-in-between links for you…

ESSENTIALLY, you just wanna do this in your controller:

@people = Person.paginate, :per\_page =\> 1, :page =\> params[:page],  
:order =\> “id ASC”

and then in your view just

\<%= will\_paginate @people, :page\_links =\> false %\>

The gem does pretty much everything for you, just take the time to  
understand the documentation and the examples, and read the source for  
anything that’s still doesn’t make sense.

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/5 "2010-02-15T22:53:19Z")

</div>

As I understand it, what he wants to do is not really pagination.  
Pagination  
is users?page=1 but what he wants is to be at user/10 and when he clicks  
next, it goes to user/11, when previous, user/9.

–Lasse

2010/2/15 EvanC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 11:04pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/6 "2010-02-15T23:04:01Z")

</div>

Lasse B. wrote:

> As I understand it, what he wants to do is not really pagination.  
> Pagination  
> is users?page=1 but what he wants is to be at user/10 and when he clicks  
> next, it goes to user/11, when previous, user/9.
> 
> –Lasse
> 
> 2010/2/15 EvanC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thats definitely still pagination. Shall we can leave it as an “exercise  
for the reader” to use the more “resourceful” type links?

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/7 "2010-02-15T22:53:54Z")

</div>

Oops, of course the url’s should be users/10, users/9 and users/11 🙂

–Lasse

2010/2/15 Lasse B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 15, 2010, 11:42pm UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/8 "2010-02-15T23:42:28Z")

</div>

Eureka!!!

Now I know why I was getting urls like users?page2. I have learned a  
lot over last few days, but please forgive me asking silly questions.  
I still get confused, perhaps I’m trying to learn to much too soon.  
Anyway, thank you all for the code examples, I will have a closer  
look at them tomorrow, after having some sleep.

Thank you very much,

Jack

---

<div class="post-metadata">

**Author:** ![bigos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bigos](https://www.ruby-forum.com/u/bigos)\
**Post date:** [February 16, 2010, 11:24am UTC](https://www.ruby-forum.com/t/will-paginate-question/183511/9 "2010-02-16T11:24:49Z")

</div>

After some thinking I finally decided to use following code:

in controller:  
@jobseeker\_previous = Jobseeker.find :all, :order =\> ‘id  
DESC’, :conditions =\> [“id \< ?”,@jobseeker.id], :limit =\> 1  
@jobseeker\_next = Jobseeker.find :all, :order =\> ‘id  
ASC’, :conditions =\> [“id \> ?”,@jobseeker.id], :limit =\> 1

in view:  
\<%@jobseeker\_previous.each do |el|%\>  
\<%= link\_to ‘Previous’, el %\>  
\<% end %\>

\<%@jobseeker\_next.each do |el|%\>  
\<%= link\_to ‘Next’, el %\>  
\<% end %\>

I tried few solutions and this one, where I get a collection  
containing one element, seems to be most convenient for handling first  
and last record, and is more idiot proof than unnecessarily having to  
test for nil object.
