rails4でブログを作成しております。
その際にviewのランキングを表示するページを作成しようと思い、
参考サイトを元にredisを利用して対応しようと考えました。
しかし、DBに認識できない文字列が登録されているというエラーが発生しました。
どなたか解決策をご教授頂ければと存じます。
※DBはPostgreSQLになります
redisは起動しており、コード変更後サーバも再起動しております。
エラー
ruby
1ActiveRecord::StatementInvalid in ArticlesController#show 2 3PG::UndefinedColumn: ERROR: column 4articles.published_at does not exist LINE 1: ...articles".* FROM "articles" WHERE (1=1) ORDER BY "articles"... ^ : 5SELECT "articles".* FROM "articles" WHERE (1=1) ORDER BY "articles"."published_at" DESC, "articles"."updated_at" DESC LIMIT 5
controllerのコード
ruby
1class ArticlesController < ApplicationController 2 before_action :set_ranking_data 3 4 def show 5 @article = Article.find(params[:id]) 6 REDIS.zincrby "articles/#{Date.today.to_s}", 1, @article.id 7 end 8 9 private 10 11 def set_ranking_data 12 ids = REDIS.zrevrangebyscore "articles/#{Date.yesterday.to_s}", "+inf", 0, limit: [0, 5] 13 @ranking_articles = ids.map{ |id| Article.find(id) } 14 15 if @ranking_articles.count < 5 16 adding_articles = Article.order(published_at: :DESC, updated_at: :DESC).where.not(id: ids).limit(5 - @ranking_articles.count) 17 @ranking_articles.concat(adding_articles) 18 end 19 end 20end 21
console上でset_ranking_dataメソッドでidsを入力するとそもそも値がnilになっております。
ruby
1>>ids = REDIS.zrevrangebyscore "articles/#{Date.yesterday.to_s}", "+inf", 0, limit: [0, 5] 2=> []
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。