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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- they''ll be able to help you better! -- Evan On Feb 15, 2:53 pm, Bigos <ruby.obj...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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 %>-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <ruby.object-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jacek Podkanski 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 (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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <evan-4NwsyH8djTSQpvdzVoCrrQ@public.gmane.org>> 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 -- they''ll be able to > help you better! > > -- Evan > > On Feb 15, 2:53 pm, Bigos <ruby.obj...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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 %> > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Oops, of course the url''s should be users/10, users/9 and users/11 :) --Lasse 2010/2/15 Lasse Bunk <lassebunk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> 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 <evan-4NwsyH8djTSQpvdzVoCrrQ@public.gmane.org> > > 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 -- they''ll be able to >> help you better! >> >> -- Evan >> >> On Feb 15, 2:53 pm, Bigos <ruby.obj...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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 %> >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Lasse Bunk 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 <evan-4NwsyH8djTSQpvdzVoCrrQ@public.gmane.org>Thats definitely still pagination. Shall we can leave it as an "exercise for the reader" to use the more "resourceful" type links? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On 15 Feb, 22:04, Paul Harrington <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Lasse Bunk 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 <e...-4NwsyH8djTSQpvdzVoCrrQ@public.gmane.org> > > Thats definitely still pagination. Shall we can leave it as an "exercise > for the reader" to use the more "resourceful" type links? > -- > Posted viahttp://www.ruby-forum.com/.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.