You can search the next and previous ids and use them to build your links
(instead of using your current ID + or - 1).
# Record/123 : display a record
get '/record/:id' do
# Get record belonging to id
@record = Record.first({id: params[:id]})
# Get id for next record
next = Record.first({:fields => [:id],
:order => :id.asc,
:id.gt => @record.id})
# (or the first record)
next = Record.first({:fields => [:id],
:order => :id.asc}) unless next
@next_id = next[:id]
# Get id for previous record
prev = Record.last({:fields => [:id],
:order => :id.asc,
:id.lt => @record.id})
# (or the last record)
prev = Record.last({:fields => [:id],
:order => :id.asc}) unless prev
@prev_id = prev[:id]
# Display the record
erb :record
end
Then you just have to use @next_id and @prev_id in your view to reach the
right url.
The "(or the first record)" part is used to move to the first record when
you are at the end (ie there is no record after the current one).
Edit :
# Get id for next record
next = Record.first({:fields => [:id],
:order => :id.asc,
:id.gt => @record.id})
We need to find the next ID after the current ID in the table. With SQL, it's :
SELECT Id # :fields => [:id]
FROM Records
WHERE Id > Current_Id # :id.gt => @record.id
ORDER BY Id ASC # :order => :id.asc
Without ":fields => [:id]", we will have a "SELECT *" when we only need the ID.