2008/06/13

Next & Previous Extension to ActiveRecord

Within Rails 2.1, ActiveRecord was added lots of flexibility, but I also want to have some next or previous functions built with the ActiveRecord model like below:

# find first foo
foo = Foo.first

# find next foo
next_foo = foo.next

# find next foo with defined sort column or conditions
next_foo = foo.next(:sort => :age, :conditions ...)

# previous function also.
previous_foo = foo.previous

#...