Anyone know of a plugin or gem that would create methods such as next
and previous on a model to traverse among siblings? For example,
@post.next and @post.prev.
you could try acts_as_list
I don’t know of any, sorry.
In the past I’ve written two simple instance methods and two
named_scopes in my models.
named_scope :next, lambda {|post, user|
{ :conditions => [“created_at > ? and user_id = ?”, post.created_at,
user.id], :order => “created_at ASC” }
}
named_scope :prev, lambda {|post, user|
{ :conditions => [“created_at < ? and user_id = ?”, post.created_at,
user.id], :order => “created_at DESC” }
}
def next(user)
Post.next(self, user).first
end
def prev(user)
Post.prev(self, user).first
end