RockPaperScissors

LizardSpock

Sinatra Blog Retrieving Next and Previous Ids

| Comments

When displaying blog/articles it is often nice to have previous and next posts. Simply -1 +1 from the current post.id is not enough because posts could be deleted or marked as private. These requirements should also be added into the :conditions statement. Below is a nice way I found of finding the previous and next ids.

get '/article/:id/?' do
   prev_post = Posts.last( :conditions => ["id < ?", params[:id]])
   next_post = Posts.first(:conditions => ["id > ?", params[:id]])
   if not prev_post.nil?
       @prev_post_id =  prev_post.id
   end
   if not next_post.nil?
      @next_post_id = next_post.id
   end

   @posts = Posts.first(:conditions => ["id = ?", params[:id]])

  erb :'blog/blog_one'
end

Comments