Active Record next and previous records

To find next and previous records according to the current record


post.rb
class Post < ActiveRecord::Base
  def next
    where("id > ?", id).first
  end

  def prev
    where("id < ?", id).last
  end
end

Share This Post