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 <[email protected]>

> 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 %>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to