I know how do find the first object that matches a set of conditions: Model.find(:first, :conditions => ["id = ?", id]) How could I find the second and third object and so on? Thanks in advance, Peter -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/11/07, Peter Marks <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I know how do find the first object that matches a set of conditions: > > Model.find(:first, :conditions => ["id = ?", id]) > > How could I find the second and third object and so on?(id=? might not be a good condition, since there will only be one row per id) You could use find(:all) and then step through them. If you want to find them one at a time, use :offset Model.find :first, :conditions => blah, :offset => 0 # first Model.find :first, :conditions => blah, :offset => 1 # second Model.find :first, :conditions => blah, :offset => 2 # third It''s basically paginating with a page size of 1 I don''t know if this is guaranteed to work for all databases. It works for MySQL. Also you probably should use :order whenever you use :offset. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Model.find :first, :conditions => blah, :offset => 0 # first > Model.find :first, :conditions => blah, :offset => 1 # second > Model.find :first, :conditions => blah, :offset => 2 # thirdYou''re right about the id condition, I was just using as an example. :offset is exactly what I was looking for though. Thanks a bunch Bob! :) -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---