参考記事を元に
rails4でredisを使ってランキング機能を実装しようと考えています。
その際showアクションでランキングデータの追加はできたのですが、呼び出し部分で値の取得ができていないようです。
お手数ですが、redisからランキングデータを取得する方法をどなたかご教授いただければと存じます
ruby
1class ArticlesController < ApplicationController 2 before_action :set_categories 3 before_action :set_ranking_data 4 5 def index 6 @articles = Article.publish.by_publish_time_desc.release_day.paginate(page: params[:page], per_page: 20) 7 end 8 9 def show 10 @article = Article.find(params[:id]) 11 REDIS.zincrby 'articles/#{Date.today.to_s}', 1, @article.id 12 end 13 14 private 15 def set_ranking_data 16 ids = REDIS.zrevrangebyscore "articles/daily/#{Date.today.to_s}", "+inf", 0, limit: [0, 5] 17 @ranking_articles = ids.map{ |id| Article.find(id) } 18 19 #@ranking_articles が空配列の為下記処理が呼び出されている 20 if @ranking_articles.count < 5 21 adding_articles = Article.order(publish_time: :DESC, updated_at: :DESC).where.not(id: ids).limit(5 - @ranking_articles.count) 22 @ranking_articles.concat(adding_articles) 23 end 24 end 25end
デバック内容
showアクションのデバック結果
9: def show 10: @article = Article.find(params[:id]) 11: REDIS.zincrby 'articles/#{Date.today.to_s}', 1, @article.id 12: byebug => 13: end (byebug) REDIS.zincrby 'articles/#{Date.today.to_s}', 1, @article.id 44.0
set_ranking_dataアクションのデバック結果
41: def set_ranking_data 42: ids = REDIS.zrevrangebyscore "articles/daily/#{Date.today.to_s}", "+inf", 0, limit: [0, 5] 43: @ranking_articles = ids.map{ |id| Article.find(id) } 44: byebug => 45: if @ranking_articles.count < 5 46: adding_articles = Article.order(publish_time: :DESC, updated_at: :DESC).where.not(id: ids).limit(5 - @ranking_articles.count) 47: @ranking_articles.concat(adding_articles) 48: end 49: end (byebug) ids []
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。