teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

試したことを追加しました

2020/01/09 07:13

投稿

.0m
.0m

スコア9

title CHANGED
File without changes
body CHANGED
@@ -123,4 +123,33 @@
123
123
  end
124
124
  end
125
125
 
126
+ ```
127
+ 試したこと
128
+ rails,redisサーバーの再起動
129
+ redis_deleteを削除し、def redis_delete
130
+ REDIS.zrem "comments", params[:id]
131
+ endを追加
132
+
133
+ ```ここに言語を入力
134
+ class Comment < ApplicationRecord
135
+ validates :user_id, {presence: true}
136
+ after_destroy do
137
+ REDIS.zrem "comments", self.id
138
+ end
139
+ def redis_access
140
+ REDIS.zincrby "comments", 1, self.id
141
+ end
142
+
143
+ def redis_page_view
144
+ REDIS.zscore("comments", self.id).floor
145
+ end
146
+
147
+ def Comment.most_popular(limit: 3)
148
+ most_popular_ids = REDIS.zrevrangebyscore "comments", "+inf", 0, limit: [0, 3]
149
+ where(id: most_popular_ids).sort_by{ |comment| most_popular_ids.index(comment.id.to_s) }
150
+ end
151
+
152
+
153
+ end
154
+
126
155
  ```