在实现 Leaderboard 中对Redis 获取的内容进行分页
Model
# leaderboard.rb
class Leaderboard
# ...
def self.top(key, limit = -1)
top = Redis.current.zrevrange(key, 0, limit, :withscores => true)
leaders = []
top.each_with_index do |e, i|
leaders << {display_name: e[0], score: e[1].to_i, rank: i + 1}
end
leaders
end
# ...
end
Controller
@leaders = Leaderboard.top(key).paginate( :page => params[:page], per_page: 10 )
这里获取到的
@leaders
为 Array
config/initializers/will_paginate_array_fix.rb
# will_paginate_array_fix.rb
require 'will_paginate/array'
创建好之后,重启 rails s
my_array.paginate(:page => x, :per_page => y)
下一篇 Rake 的一些命令