# \[ANN\] acts\_as\_ordered plugin

**URL:** <https://www.ruby-forum.com/t/ann-acts-as-ordered-plugin/56337>\
**Category:** Rails\
**Created:** [March 30, 2006, 9:26am UTC](https://www.ruby-forum.com/t/ann-acts-as-ordered-plugin/56337 "2006-03-30T09:26:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 30, 2006, 9:26am UTC](https://www.ruby-forum.com/t/ann-acts-as-ordered-plugin/56337/1 "2006-03-30T09:26:47Z")

</div>

Hey all,

I’ve just put together a little plugin to easily access the neighbours  
of a  
given model in a particular order. Read on if interested.

Install:  
script/plugin install  
[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_ordered](http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered)

Usage:  
class Person \< ActiveRecord::Base  
acts\_as\_ordered :order =\> ‘first\_name’  
end

Say you have people with first names of Adam, Jonathan and Robert:

p = Person.find\_by\_first\_name(‘Jonathan’)

p.next # Robert  
p.previous # Adam  
p.previous.previous # Adam (does not wrap around to end)  
p.next.next # Robert (does not wrap around to start)

If you want the next and previous methods to wrap around the ends, use:

acts\_as\_ordered :order =\> ‘first\_name’, :wrap =\> true

You can use this in a application to be able to quickly browse through  
people. Eg:

class PersonController \< ApplicationController  
def view\_neighbour  
# params[:direction] is passed off the page as ‘next’ or  
‘previous’  
@person = Person.find(params[:id]).send(params[:direction])  
render :action =\> ‘view’  
end  
end

Problems, comments, and suggestions all welcome.

Cheers, -Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 30, 2006, 11:56am UTC](https://www.ruby-forum.com/t/ann-acts-as-ordered-plugin/56337/2 "2006-03-30T11:56:10Z")

</div>

I’ve just added the ability to jump by more than one at a time.

Say you have people with first names of Adam, Jonathan and Robert:

# Without wrapping (clamps to ends)

p = Person.find\_by\_first\_name(‘Jonathan’)  
p.next(2) # Robert  
p.next(100) # Robert  
p.previous(100) # Jonathan

# With wrapping (wraps around ends as many times as you like)

p = Person.find\_by\_first\_name(‘Jonathan’)  
p.next(8) # Adam  
p.previous(17) # Robert

It will clamp or wrap around the ends depending on whether or not you  
specify :wrap =\> true.

Cheers, -Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [March 30, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/ann-acts-as-ordered-plugin/56337/3 "2006-03-30T21:40:51Z")

</div>

Wow! That’s really cool. Thanks,
