# Next and previous methods for a model

**URL:** <https://www.ruby-forum.com/t/next-and-previous-methods-for-a-model/153782>\
**Category:** Rails\
**Created:** [November 21, 2008, 12:50am UTC](https://www.ruby-forum.com/t/next-and-previous-methods-for-a-model/153782 "2008-11-21T00:50:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 21, 2008, 12:50am UTC](https://www.ruby-forum.com/t/next-and-previous-methods-for-a-model/153782/1 "2008-11-21T00:50:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 21, 2008, 9:44am UTC](https://www.ruby-forum.com/t/next-and-previous-methods-for-a-model/153782/2 "2008-11-21T09:44:27Z")

</div>

you could try acts\_as\_list

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 21, 2008, 8:37am UTC](https://www.ruby-forum.com/t/next-and-previous-methods-for-a-model/153782/3 "2008-11-21T08:37:03Z")

</div>

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
