I’m trying to add Prevous/Next links in my show view. Here is the model:
Position
belongs_to :skill
Skill
has_many :positions, :order => 'salary desc, id desc'
position/show view:
<%= link_to("Previous", @position.previous) if @position.previous %>
<%= link_to("Next", @position.next) if @position.next %>
position.rb (new lines added for readibility)
def next
self.class
.where("skill_id = ? AND salary <= ? AND id < ?", skill_id, salary, id)
.order("salary desc, id desc").first
end
This doesn’t do what I want. Records should be ordered first by salary, and than by id.
I think will_paginate won’t help me because it’s only for collections (won’t work in the show view)
I found the solution 🙂 It’s rather long, but it works: