Next/Previous record?

Is there a way to get the next or previous record from a table or from
a model?

Thanks,
John

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 B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs