On Sep 16, 2007, at 10:50 PM, Jables wrote:
>
> Is there a way to get the next or previous record from a table or from
> a model?
>
> Thanks,
> John
This is how I did it for one recent client:
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
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---