讓我們增加一些新的東西進入你的Video
型號:
class Video < ActiveRecord::Base
def next(column = 'votes_count', order = 'DESC')
pos = self.position(column, order)
on_position(pos - 1, column, order) if pos > 0
end
def previous(column = 'votes_count', order = 'DESC')
pos = self.position(column, order)
on_position(pos + 1, column, order)
end
private
def position(column, order)
order_by = "#{column} #{order}"
arrow = order.capitalize == "ASC" ? "<=" : "=>"
Video.where("#{column} #{arrow} (?)", self.send(column)).order(order_by).count
end
def on_position(pos, column, order)
order_by = "#{column} #{order}"
arrow = order.capitalize == "ASC" ? "<=" : "=>"
Video.order(order_by).offset(pos).first
end
end
而且你的控制器
def show
@video = Video.find(params[:id])
@next_video = @video.next
@previous_video = @video.previous
end
注意,我沒有測試過它,所以它可能是馬車。所有錯誤放在評論:)
但你想如何工作? – fl00r 2011-04-20 18:43:40