I have scopes defined in an include module like this:
module ActsAsAdjacent
def self.included(base)
base.send(:scope, :next, lambda {|id| {:conditions => ["id > ?",id],
:order => "id ASC", :limit => 1}})
base.send(:scope, :previous, lambda {|id| {:conditions => ["id < ?",id],
:order => "id DESC", :limit => 1}})
end
, the idea being that you can mixin next, previous capability into any ActiveRecord::Base subclass. (The above is a variant of some examples floating around).
The obvious draw back to the above though is that it is assuming a specific column, 'id', whereas that column may not exist in some instances, or there may be a different sort order being used.
Is there a way to do next, previous based on whatever the current sort order is, or how ever the records are layed out in the db.
So for example in the following case,
class User < ActiveRecord::Base
default_scope :order => 'users.name ASC'
include ActsAsAdjacent
ActsAsAdjacent would automatically use that sort order to return next, previous.
I guess id will always exist, but it may not be sorted on that.