To implement something like Model.previous, the class itself must have a "current" state. This would make sense if the βcurrentβ record (perhaps in the publication planning system?) Were Record3 in your example, but your example does not offer this.
If you want to take an instance of the model and get the next or previous record, the following example:
class Page < ActiveRecord::Base def previous(offset = 0) self.class.first(:conditions => ['id < ?', self.id], :limit => 1, :offset => offset, :order => "id DESC") end def next(offset = 0) self.class.first(:conditions => ['id > ?', self.id], :limit => 1, :offset => offset, :order => "id ASC") end end
If so, you can do something like:
@page = Page.find(4) @page.previous
Also would work:
@page.previous(1) @page.next @page.next(1)
Obviously, this suggests that the idea of βββnextβ and βpreviousβ is an βidβ field that probably will not spread very well throughout the life of the application.
If you want to use this in a class, perhaps you can extend it in a named scope that takes the "current" record as an argument. Something like that:
named_scope :previous, lambda { |current, offset| { :conditions => ['id < ?', current], :limit => 1, :offset => offset, :order => "id DESC" }}
This means you could call:
Page.previous(4,1)
Where "4" is the identifier of the entry you want to start with, and 1 is the number you want to go back to.