I just finished a plugin called “records_sequence” which add “next/
previous” methods to each ActiveRecord object. After installed, it
could be a pleasure to find a object’s neighbors.
== INSTALL ==
This plugin requires Rails 2.1+ currently. Install it by below
command:
ruby script/plugin install
git://github.com/Aaron2Ti/records_sequence.git
== USAGE ==
After installed, your models could:
foo = User.first
bar = foo.next
bar2 = foo.previous
foo_neighbour = foo.previous # returns nil if foo has no previous
neighbour
#The sequence’s sorted column is defined by the new
option :sorted_by,
default is the ‘id’ column.
foo = User.last
pre_foo_sorted_by_id = foo.previous
pre_foo_sorted_by_age = foo.previous(:sorted_by => ‘age’)
#Also works fine with most other ActiveRecord’s find options:
foo = User.find 30
foo.next(:conditions => [‘age < ?’, 20])
foo.previous(:offset => 2)
foo.next(:sorted_by => ‘address’, :order => ‘name DESC, age’)
foo.previous( :sorted_by => ‘age’, :conditions => [‘age < ?’, 20],
:order => ‘name DESC, address’, :offset => 2 )
Future info of this plugin also could be found at my blog: