thanks colin and rob, that's approximately what I have so far, I was hoping a solution to do it in one request :-/
On Sep 23, 3:08 pm, Rob Biedenharn <r...@agileconsultingllc.com> wrote: > On Sep 23, 2009, at 7:49 AM, mickeyben wrote: > > > > > > > On Sep 23, 2:38 pm, Colin Law <clan...@googlemail.com> wrote: > >> 2009/9/23 Bensoussan Michael <pad...@gmail.com>: > >>> Hi, > >>> How would you get an SQL element, the next one and the previous one, > >>> but the definition of a next/previous element is not dependant > >>> only of > >>> his id but also of a status, > >>> example : > > >>> id status > >>> 1 true > >>> 2 false > >>> 3 true > > >>> Here the next element of 1 is 3 > > >> If I understand correctly you can specify an order in the find call > >> that provides the records in whatever order you want. > > >> Note also that if you do not specify an order then there is no > >> guarantee in what order they will appear. They will not necessarily > >> be in id order. > > >> Colin > > > yes but that doesnt resolve my problem (I think), I don't want to > > select all the database, otherwise I just need to filter by status an > > order by id, I just want this 3 results :-/ > > This might help. These are some class methods to do something similar. > > def self.prior_to id > find(:first, :conditions => ["#{primary_key} < ?", id], :order => > "#{primary_key} DESC") || > find(:first, :conditions => ["#{primary_key} >= ?", id], :order > => "#{primary_key}") > end > > def self.next_after id > find(:first, :conditions => ["#{primary_key} > ?", id], :order => > "#{primary_key}") || > find(:first, :conditions => ["#{primary_key} <= ?", id], :order > => "#{primary_key} DESC") > end > > You'd want to adjust the conditions to also look at status and > probably take a model instance rather than an id. Maybe even a list of > the attributes that must be matched (just :status in your example). > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > r...@agileconsultingllc.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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---