I need to get the previous and next active record objects with Rails. I did it, but don\'t know if it\'s the right way to do that.
What I\'ve got:
Controller
Write these methods in your Product model:
class Product
def next
self.class.where("id > ?", id).first
end
def previous
self.class.where("id < ?", id).last
end
end
Now you can do in your controller:
@product = Product.friendly.find(params[:id])
@previous_product = @product.next
@next_product = @product.previous
Please try it, but its not tested. Thanks