0

I am working in Padrino and have this in my controller

@work.find_by_id(params[:id])

I want to add prev/next buttons into my view, and so must be able to get the path to the next item on the list. How can I do that with ActiveRecord?

picardo
  • 24,530
  • 33
  • 104
  • 151

2 Answers2

0

You may want to checkout the ActsAsAdjacent plugin. I'm not sure if you can use plugins with Padrino, but the code is pretty simple, and should be able to adapt to your needs.

Kayla Rose
  • 1,293
  • 10
  • 12
0

I use a PreviousNextable module like so:

module PreviousNextable  

  def self.included(klass)

    klass.class_eval do
      extend ActiveSupport::Memoizable 
      memoize :previous
      memoize :next
    end

  end  

  def previous
    self.class.last :order => 'name', :conditions => ['name < ?', self.name]
  end

  def next
    self.class.first :order => 'name', :conditions => ['name > ?', self.name]
  end

end

This code was adapted from an SO answer given by Ryan Bates. Your order and conditions to determine what's previous or next will likely differ. Probably you'll want to use 'created_at'.

Include the module in your model class:

class Place < ActiveRecord::Base
  include PreviousNextable  

and you're off.

Mark Holland
  • 846
  • 4
  • 8