質問編集履歴

7

解決したコード及びER図を追加。

2020/12/18 09:02

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -257,3 +257,103 @@
257
257
  RubyGems 3.0.3
258
258
 
259
259
  Rails 5.2.3
260
+
261
+
262
+
263
+ ### 【追記】ER図
264
+
265
+ ![イメージ説明](3eb28b15259792379cb3515f2c257132.jpeg)
266
+
267
+
268
+
269
+ ### 【追記】解決したコード
270
+
271
+
272
+
273
+ ### rooms_controller.rb
274
+
275
+
276
+
277
+ ```Ruby
278
+
279
+ def index
280
+
281
+ #以下、メッセージ機能に関する記述。変更なし。
282
+
283
+ @rooms = Room.page(params[:page]).per(3)
284
+
285
+ @user = @current_user
286
+
287
+ @currentEntries = @current_user.entries
288
+
289
+ myRoomIds = []
290
+
291
+ @currentEntries.each do | entry |
292
+
293
+ myRoomIds << entry.room_id
294
+
295
+ end
296
+
297
+ @anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
298
+
299
+
300
+
301
+ # 既読機能についてはコントローラでは特に記述せず。ビューにて記述。
302
+
303
+ end
304
+
305
+
306
+
307
+ def show
308
+
309
+ #以下、メッセージ機能に関する記述。変更なし。
310
+
311
+ @rooms = Room.page(params[:page]).per(3)
312
+
313
+ @user = @current_user
314
+
315
+ @currentEntries = @current_user.entries
316
+
317
+ myRoomIds = []
318
+
319
+ @currentEntries.each do | entry |
320
+
321
+ myRoomIds << entry.room_id
322
+
323
+ end
324
+
325
+ @anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
326
+
327
+
328
+
329
+ # ここから既読機能。ログイン中のユーザーが抱える「message」に関する「相手からの通知」のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
330
+
331
+ @current_user.passive_notifications.where(action: 'message', read: false, room_id: params[:id]).each do |notification|
332
+
333
+ notification.update_attributes(read: true)
334
+
335
+ end
336
+
337
+ end
338
+
339
+ ```
340
+
341
+
342
+
343
+ ### rooms_index.html.erb と rooms_show.html.erb(既読機能に関する内容は同じ)
344
+
345
+
346
+
347
+ ```Rails
348
+
349
+ <!-- ログイン中のユーザーが保持している各Entryの中で、未読メッセージがある場合countを繰り返し表示。-->
350
+
351
+ <% message_count = Notification.where(action: "message", visited_id: @current_user.id, visitor_id: e.user.id, room_id: e.room.id, read: false).count %>
352
+
353
+ <% if message_count > 0 %>
354
+
355
+ <div class="message-count date-and-count"><%= message_count %></div>
356
+
357
+ <% end %>
358
+
359
+ ```

6

rooms_controller.rb内のindexに追記。

2020/12/18 09:01

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -162,12 +162,20 @@
162
162
 
163
163
  @anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
164
164
 
165
+
166
+
167
+ @notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
168
+
169
+ @notifications.where(checked: false).each do |notification|
170
+
171
+ notification.update_attributes(checked: true)
172
+
173
+ end
174
+
165
175
  @unread = @notifications.where(room_id: params[:id], read: false).count
166
176
 
167
177
  end
168
178
 
169
-
170
-
171
179
  ```
172
180
 
173
181
 

5

現状のコードに変更。エラー文は出なくなったので、エラー文は削除。

2020/12/15 12:30

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -134,7 +134,7 @@
134
134
 
135
135
  # ログイン中のユーザーが抱える相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
136
136
 
137
- @current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
137
+ @current_user.passive_notifications.find_by(params[:id]).update(read: true)
138
138
 
139
139
  # 該当のroom_id内にある未読メッセージの数を取得。
140
140
 
@@ -146,6 +146,22 @@
146
146
 
147
147
  def index
148
148
 
149
+ @rooms = Room.page(params[:page]).per(3)
150
+
151
+ @user = @current_user
152
+
153
+ @currentEntries = @current_user.entries
154
+
155
+ myRoomIds = []
156
+
157
+ @currentEntries.each do | entry |
158
+
159
+ myRoomIds << entry.room_id
160
+
161
+ end
162
+
163
+ @anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
164
+
149
165
  @unread = @notifications.where(room_id: params[:id], read: false).count
150
166
 
151
167
  end
@@ -162,7 +178,63 @@
162
178
 
163
179
  ```Rails
164
180
 
181
+ <div class="message-items">
182
+
183
+ <% @anotherEntries.each do |e| %>
184
+
185
+ <div class="message-item">
186
+
187
+ <div class="message-left">
188
+
189
+ <%= link_to room_path(e.room.id) do %>
190
+
191
+ <img src="<%= "/user_images/#{e.user.image_name}" %>">
192
+
193
+ <% end %>
194
+
195
+ </div>
196
+
197
+ <div class="message-center">
198
+
199
+ <div class="message-center-username">
200
+
201
+ <%= link_to room_path(e.room.id) do %
202
+
203
+ <%= e.user.name %>
204
+
165
- <%= @unread %>
205
+ <%= @unread %>
206
+
207
+ <% end %>
208
+
209
+ </div>
210
+
211
+ <div class="message-center-message">
212
+
213
+ <%= link_to room_path(e.room.id) do %>
214
+
215
+ <% dm = Message.find_by(id: e.room.message_ids.last).try(:content) %>
216
+
217
+ <%= truncate(dm, length: 9) %>
218
+
219
+ <% end %>
220
+
221
+ </div>
222
+
223
+ </div>
224
+
225
+ <div class="message-right">
226
+
227
+ <div class="message-date" style="color: #C0C0C0;"><%= e.updated_at.strftime("%Y/%m/%d") %></div>
228
+
229
+ <div class="message-time" style="color: #C0C0C0;"><%= e.updated_at.strftime("%H:%M") %></div>
230
+
231
+ </div>
232
+
233
+ </div>
234
+
235
+ <% end %>
236
+
237
+ </div>
166
238
 
167
239
  ```
168
240
 
@@ -177,171 +249,3 @@
177
249
  RubyGems 3.0.3
178
250
 
179
251
  Rails 5.2.3
180
-
181
-
182
-
183
- ### 追記
184
-
185
-
186
-
187
- 上記の記述ですと、`rooms_index.html.erb`は正常に表示されるものの`rooms_show.html.erb`を開くと以下のエラーが表示されます。
188
-
189
-
190
-
191
- ```
192
-
193
- NoMethodError in RoomsController#show
194
-
195
- undefined method `update' for nil:NilClass
196
-
197
- Extracted source (around line #51):
198
-
199
- 49 # 「message」に関する「相手からの通知」のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
200
-
201
- 50 # @notifications.find_by(room_id: params[:id]).update(read: true)
202
-
203
- 51 @current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
204
-
205
- 52
206
-
207
- 53 # 該当のroom_id内にある未読メッセージの数を取得。→rooms_show.html.erb
208
-
209
- 54 # @unread = @notifications.find_by(room_id: params[:id]).where(read: false).count
210
-
211
- ```
212
-
213
-
214
-
215
- またその際のログは以下の様になっています。
216
-
217
-
218
-
219
- ```
220
-
221
- app/controllers/rooms_controller.rb:51:in `show'
222
-
223
- Started GET "/rooms/10" for ::1 at 2020-12-15 18:36:25 +0900
224
-
225
- Processing by RoomsController#show as HTML
226
-
227
- Parameters: {"id"=>"10"}
228
-
229
- User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
230
-
231
- ↳ app/controllers/application_controller.rb:9
232
-
233
- Entry Load (0.3ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
234
-
235
- ↳ app/controllers/rooms_controller.rb:24
236
-
237
- Room Load (0.2ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
238
-
239
- ↳ app/controllers/rooms_controller.rb:31
240
-
241
- Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
242
-
243
- ↳ app/controllers/rooms_controller.rb:32
244
-
245
- Notification Load (0.6ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."room_id" = ? LIMIT ? [["visited_id", 20], ["room_id", 10], ["LIMIT", 1]]
246
-
247
- ↳ app/controllers/rooms_controller.rb:51
248
-
249
- Completed 500 Internal Server Error in 144ms (ActiveRecord: 8.7ms)
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
- NoMethodError (undefined method `update' for nil:NilClass):
258
-
259
- ```
260
-
261
-
262
-
263
- また、`rooms_index.html.erb`を開いた際のログはこちらです。
264
-
265
-
266
-
267
- ```
268
-
269
- app/controllers/rooms_controller.rb:51:in `show'
270
-
271
- Started GET "/rooms/index" for ::1 at 2020-12-15 18:41:37 +0900
272
-
273
- Processing by RoomsController#index as HTML
274
-
275
- User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
276
-
277
- ↳ app/controllers/application_controller.rb:9
278
-
279
- Entry Load (0.4ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
280
-
281
- ↳ app/controllers/rooms_controller.rb:71
282
-
283
- Notification Load (0.5ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
284
-
285
- ↳ app/controllers/rooms_controller.rb:86
286
-
287
- (0.8ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
288
-
289
- ↳ app/controllers/rooms_controller.rb:93
290
-
291
- Rendering rooms/index.html.erb within layouts/application
292
-
293
- Entry Load (1.6ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
294
-
295
- ↳ app/views/rooms/index.html.erb:11
296
-
297
- User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
298
-
299
- ↳ app/views/rooms/index.html.erb:11
300
-
301
- Room Load (0.7ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
302
-
303
- ↳ app/views/rooms/index.html.erb:11
304
-
305
- (0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
306
-
307
- ↳ app/views/rooms/index.html.erb:30
308
-
309
- Message Load (0.7ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
310
-
311
- ↳ app/views/rooms/index.html.erb:30
312
-
313
- (0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
314
-
315
- ↳ app/views/rooms/index.html.erb:30
316
-
317
- Message Load (0.3ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
318
-
319
- ↳ app/views/rooms/index.html.erb:30
320
-
321
- Rendered rooms/index.html.erb within layouts/application (68.2ms)
322
-
323
- CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
324
-
325
- ↳ app/views/layouts/application.html.erb:39
326
-
327
- (0.5ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."checked" = ? AND "notifications"."action" = ? [["visited_id", 20], ["checked", 0], ["action", "message"]]
328
-
329
- ↳ app/helpers/notifications_helper.rb:47
330
-
331
- (1.0ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
332
-
333
- ↳ app/helpers/notifications_helper.rb:41
334
-
335
- Completed 200 OK in 321ms (Views: 260.2ms | ActiveRecord: 10.3ms)
336
-
337
-
338
-
339
-
340
-
341
- Started GET "/rooms/js/tag.js" for ::1 at 2020-12-15 18:41:37 +0900
342
-
343
-
344
-
345
- ActionController::RoutingError (No route matches [GET] "/rooms/js/tag.js"):
346
-
347
- ```

4

rooms_controller.rb内及びエラー文を最新のものに書き換え。

2020/12/15 10:59

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -132,9 +132,9 @@
132
132
 
133
133
  @notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
134
134
 
135
- # 「message」に関する相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
135
+ # ログイン中のユーザーが抱える相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
136
-
136
+
137
- @notifications.find_by(room_id: params[:id]).update(read: true)
137
+ @current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
138
138
 
139
139
  # 該当のroom_id内にある未読メッセージの数を取得。
140
140
 
@@ -194,113 +194,131 @@
194
194
 
195
195
  undefined method `update' for nil:NilClass
196
196
 
197
-
197
+ Extracted source (around line #51):
198
+
198
-
199
+ 49 # 「message」に関する「相手からの通知」のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
200
+
199
- @notifications.find_by(room_id: params[:id]).update(read: true)
201
+ 50 # @notifications.find_by(room_id: params[:id]).update(read: true)
202
+
200
-
203
+ 51 @current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
204
+
205
+ 52
206
+
207
+ 53 # 該当のroom_id内にある未読メッセージの数を取得。→rooms_show.html.erb
208
+
209
+ 54 # @unread = @notifications.find_by(room_id: params[:id]).where(read: false).count
210
+
201
- ```
211
+ ```
202
-
203
-
204
-
212
+
213
+
214
+
205
- 何かパラメータが上手く渡っていないのか…。その際のログは以下の様になっています。
215
+ またその際のログは以下の様になっています。
206
-
207
-
208
-
216
+
217
+
218
+
209
- ```
219
+ ```
220
+
210
-
221
+ app/controllers/rooms_controller.rb:51:in `show'
222
+
211
- Started GET "/rooms/10" for ::1 at 2020-12-13 23:19:49 +0900
223
+ Started GET "/rooms/10" for ::1 at 2020-12-15 18:36:25 +0900
212
224
 
213
225
  Processing by RoomsController#show as HTML
214
226
 
215
227
  Parameters: {"id"=>"10"}
216
228
 
217
- User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
229
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
218
230
 
219
231
  ↳ app/controllers/application_controller.rb:9
220
232
 
233
+ Entry Load (0.3ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
234
+
235
+ ↳ app/controllers/rooms_controller.rb:24
236
+
237
+ Room Load (0.2ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
238
+
239
+ ↳ app/controllers/rooms_controller.rb:31
240
+
241
+ Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
242
+
243
+ ↳ app/controllers/rooms_controller.rb:32
244
+
245
+ Notification Load (0.6ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."room_id" = ? LIMIT ? [["visited_id", 20], ["room_id", 10], ["LIMIT", 1]]
246
+
247
+ ↳ app/controllers/rooms_controller.rb:51
248
+
249
+ Completed 500 Internal Server Error in 144ms (ActiveRecord: 8.7ms)
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+ NoMethodError (undefined method `update' for nil:NilClass):
258
+
259
+ ```
260
+
261
+
262
+
263
+ また、`rooms_index.html.erb`を開いた際のログはこちらです。
264
+
265
+
266
+
267
+ ```
268
+
269
+ app/controllers/rooms_controller.rb:51:in `show'
270
+
271
+ Started GET "/rooms/index" for ::1 at 2020-12-15 18:41:37 +0900
272
+
273
+ Processing by RoomsController#index as HTML
274
+
275
+ User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
276
+
277
+ ↳ app/controllers/application_controller.rb:9
278
+
221
279
  Entry Load (0.4ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
222
280
 
223
- ↳ app/controllers/rooms_controller.rb:24
224
-
225
- Room Load (0.4ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
226
-
227
- ↳ app/controllers/rooms_controller.rb:31
228
-
229
- Entry Load (0.7ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
230
-
231
- ↳ app/controllers/rooms_controller.rb:32
232
-
233
- Notification Load (0.7ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["room_id", 10], ["LIMIT", 1], ["OFFSET", 0]]
234
-
235
- ↳ app/controllers/rooms_controller.rb:50
236
-
237
- Completed 500 Internal Server Error in 143ms (ActiveRecord: 10.4ms)
238
-
239
-
240
-
241
- NoMethodError (undefined method `update' for nil:NilClass):
242
-
243
- ```
244
-
245
-
246
-
247
- また、`rooms_index.html.erb`を開いた際のログはこちらです。
248
-
249
-
250
-
251
- ```
252
-
253
- Started GET "/rooms/index" for ::1 at 2020-12-13 23:23:45 +0900
254
-
255
- Processing by RoomsController#index as HTML
256
-
257
- User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
258
-
259
- ↳ app/controllers/application_controller.rb:9
260
-
261
- Entry Load (0.2ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
262
-
263
- ↳ app/controllers/rooms_controller.rb:70
264
-
265
- Notification Load (3.0ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
266
-
267
- ↳ app/controllers/rooms_controller.rb:85
268
-
269
- (2.2ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
270
-
271
- ↳ app/controllers/rooms_controller.rb:92
281
+ ↳ app/controllers/rooms_controller.rb:71
282
+
283
+ Notification Load (0.5ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
284
+
285
+ ↳ app/controllers/rooms_controller.rb:86
286
+
287
+ (0.8ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
288
+
289
+ ↳ app/controllers/rooms_controller.rb:93
272
290
 
273
291
  Rendering rooms/index.html.erb within layouts/application
274
292
 
275
- Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
293
+ Entry Load (1.6ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
276
294
 
277
295
  ↳ app/views/rooms/index.html.erb:11
278
296
 
279
- User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
297
+ User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
280
298
 
281
299
  ↳ app/views/rooms/index.html.erb:11
282
300
 
283
- Room Load (3.4ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
301
+ Room Load (0.7ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
284
302
 
285
303
  ↳ app/views/rooms/index.html.erb:11
286
304
 
287
- (0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
305
+ (0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
288
306
 
289
307
  ↳ app/views/rooms/index.html.erb:30
290
308
 
291
- Message Load (0.4ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
309
+ Message Load (0.7ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
292
310
 
293
311
  ↳ app/views/rooms/index.html.erb:30
294
312
 
295
- (0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
313
+ (0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
296
314
 
297
315
  ↳ app/views/rooms/index.html.erb:30
298
316
 
299
- Message Load (0.2ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
317
+ Message Load (0.3ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
300
318
 
301
319
  ↳ app/views/rooms/index.html.erb:30
302
320
 
303
- Rendered rooms/index.html.erb within layouts/application (42.9ms)
321
+ Rendered rooms/index.html.erb within layouts/application (68.2ms)
304
322
 
305
323
  CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
306
324
 
@@ -310,10 +328,20 @@
310
328
 
311
329
  ↳ app/helpers/notifications_helper.rb:47
312
330
 
313
- (0.6ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
331
+ (1.0ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
314
332
 
315
333
  ↳ app/helpers/notifications_helper.rb:41
316
334
 
317
- Completed 200 OK in 371ms (Views: 325.4ms | ActiveRecord: 13.1ms)
335
+ Completed 200 OK in 321ms (Views: 260.2ms | ActiveRecord: 10.3ms)
336
+
337
+
338
+
339
+
340
+
318
-
341
+ Started GET "/rooms/js/tag.js" for ::1 at 2020-12-15 18:41:37 +0900
342
+
343
+
344
+
345
+ ActionController::RoutingError (No route matches [GET] "/rooms/js/tag.js"):
346
+
319
- ```
347
+ ```

3

「追記」を記入

2020/12/15 09:45

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -134,14 +134,24 @@
134
134
 
135
135
  # 「message」に関する相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
136
136
 
137
- @notifications.find_by(room_id: params[:id]).update_attributes(read: true)
137
+ @notifications.find_by(room_id: params[:id]).update(read: true)
138
138
 
139
139
  # 該当のroom_id内にある未読メッセージの数を取得。
140
140
 
141
- @unread = @notifications.find_by(room_id: params[:id]).where(read: false, action: 'message').count
141
+ @unread = @notifications.where(room_id: params[:id], read: false).count
142
142
 
143
143
  end
144
144
 
145
+
146
+
147
+ def index
148
+
149
+ @unread = @notifications.where(room_id: params[:id], read: false).count
150
+
151
+ end
152
+
153
+
154
+
145
155
  ```
146
156
 
147
157
 
@@ -167,3 +177,143 @@
167
177
  RubyGems 3.0.3
168
178
 
169
179
  Rails 5.2.3
180
+
181
+
182
+
183
+ ### 追記
184
+
185
+
186
+
187
+ 上記の記述ですと、`rooms_index.html.erb`は正常に表示されるものの`rooms_show.html.erb`を開くと以下のエラーが表示されます。
188
+
189
+
190
+
191
+ ```
192
+
193
+ NoMethodError in RoomsController#show
194
+
195
+ undefined method `update' for nil:NilClass
196
+
197
+
198
+
199
+ @notifications.find_by(room_id: params[:id]).update(read: true)
200
+
201
+ ```
202
+
203
+
204
+
205
+ 何かパラメータが上手く渡っていないのか…。その際のログは以下の様になっています。
206
+
207
+
208
+
209
+ ```
210
+
211
+ Started GET "/rooms/10" for ::1 at 2020-12-13 23:19:49 +0900
212
+
213
+ Processing by RoomsController#show as HTML
214
+
215
+ Parameters: {"id"=>"10"}
216
+
217
+ User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
218
+
219
+ ↳ app/controllers/application_controller.rb:9
220
+
221
+ Entry Load (0.4ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
222
+
223
+ ↳ app/controllers/rooms_controller.rb:24
224
+
225
+ Room Load (0.4ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
226
+
227
+ ↳ app/controllers/rooms_controller.rb:31
228
+
229
+ Entry Load (0.7ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
230
+
231
+ ↳ app/controllers/rooms_controller.rb:32
232
+
233
+ Notification Load (0.7ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["room_id", 10], ["LIMIT", 1], ["OFFSET", 0]]
234
+
235
+ ↳ app/controllers/rooms_controller.rb:50
236
+
237
+ Completed 500 Internal Server Error in 143ms (ActiveRecord: 10.4ms)
238
+
239
+
240
+
241
+ NoMethodError (undefined method `update' for nil:NilClass):
242
+
243
+ ```
244
+
245
+
246
+
247
+ また、`rooms_index.html.erb`を開いた際のログはこちらです。
248
+
249
+
250
+
251
+ ```
252
+
253
+ Started GET "/rooms/index" for ::1 at 2020-12-13 23:23:45 +0900
254
+
255
+ Processing by RoomsController#index as HTML
256
+
257
+ User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
258
+
259
+ ↳ app/controllers/application_controller.rb:9
260
+
261
+ Entry Load (0.2ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
262
+
263
+ ↳ app/controllers/rooms_controller.rb:70
264
+
265
+ Notification Load (3.0ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
266
+
267
+ ↳ app/controllers/rooms_controller.rb:85
268
+
269
+ (2.2ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
270
+
271
+ ↳ app/controllers/rooms_controller.rb:92
272
+
273
+ Rendering rooms/index.html.erb within layouts/application
274
+
275
+ Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
276
+
277
+ ↳ app/views/rooms/index.html.erb:11
278
+
279
+ User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
280
+
281
+ ↳ app/views/rooms/index.html.erb:11
282
+
283
+ Room Load (3.4ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
284
+
285
+ ↳ app/views/rooms/index.html.erb:11
286
+
287
+ (0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
288
+
289
+ ↳ app/views/rooms/index.html.erb:30
290
+
291
+ Message Load (0.4ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
292
+
293
+ ↳ app/views/rooms/index.html.erb:30
294
+
295
+ (0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
296
+
297
+ ↳ app/views/rooms/index.html.erb:30
298
+
299
+ Message Load (0.2ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
300
+
301
+ ↳ app/views/rooms/index.html.erb:30
302
+
303
+ Rendered rooms/index.html.erb within layouts/application (42.9ms)
304
+
305
+ CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
306
+
307
+ ↳ app/views/layouts/application.html.erb:39
308
+
309
+ (0.5ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."checked" = ? AND "notifications"."action" = ? [["visited_id", 20], ["checked", 0], ["action", "message"]]
310
+
311
+ ↳ app/helpers/notifications_helper.rb:47
312
+
313
+ (0.6ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
314
+
315
+ ↳ app/helpers/notifications_helper.rb:41
316
+
317
+ Completed 200 OK in 371ms (Views: 325.4ms | ActiveRecord: 13.1ms)
318
+
319
+ ```

2

rooms_controller.rb及びrooms_index.html.erbを現在の状態に書き直し。

2020/12/13 14:42

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -124,45 +124,23 @@
124
124
 
125
125
 
126
126
 
127
- この記述ですと、どの`room`を開いても全ての`message`レコードが`read: true`になってしまいます…。
128
-
129
127
  ```Ruby
130
128
 
131
129
  def show
132
130
 
133
- # ログイン中のユーザーが抱える「message」に関する相手からの通知を取得。
131
+ # ログイン中のユーザーが抱える「message」に関する相手からの通知を取得。
134
132
 
135
133
  @notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
136
134
 
137
- @notifications.where(read: false).each do |notification|
135
+ # 「message」に関する相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
138
136
 
139
- # rooms/:idを表示した時点で、それらのレコードを確認済み(checked: true)に変更。
137
+ @notifications.find_by(room_id: params[:id]).update_attributes(read: true)
140
138
 
141
- notification.update_attributes(read: true)
139
+ # 該当のroom_id内にある未読メッセージの数を取得。
142
140
 
143
- end
141
+ @unread = @notifications.find_by(room_id: params[:id]).where(read: false, action: 'message').count
144
142
 
145
143
  end
146
-
147
- ```
148
-
149
-
150
-
151
- ### notifications_helper.rb
152
-
153
-
154
-
155
- この記述ですと、未読メッセージが全部でいくつあるかが取得され、各room内における個別の件数が分かりません。
156
-
157
- ```Ruby
158
-
159
- #ログイン中のユーザーに、「message」に関する未読(=read: false)の通知が「いくつあるか」を判別。
160
-
161
- def unread_message_notifications_count
162
-
163
- @unread = @current_user.passive_notifications.where(read: false, action: 'message').count
164
-
165
- end
166
144
 
167
145
  ```
168
146
 
@@ -174,7 +152,7 @@
174
152
 
175
153
  ```Rails
176
154
 
177
- <%= unread_message_notifications_count %>
155
+ <%= @unread %>
178
156
 
179
157
  ```
180
158
 

1

helper.rb内、index.html.erb内の記述を編集。

2020/12/13 10:39

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -156,13 +156,13 @@
156
156
 
157
157
  ```Ruby
158
158
 
159
- #ログイン中のユーザーに、「message」に関する未読(=checked: false)の通知が「いくつあるか」を判別。
159
+ #ログイン中のユーザーに、「message」に関する未読(=read: false)の通知が「いくつあるか」を判別。
160
160
 
161
- def unchecked_message_notifications_count
161
+ def unread_message_notifications_count
162
162
 
163
- @m_notifications = @current_user.passive_notifications.where(checked: false, action: 'message').count
163
+ @unread = @current_user.passive_notifications.where(read: false, action: 'message').count
164
164
 
165
- end
165
+ end
166
166
 
167
167
  ```
168
168
 
@@ -174,11 +174,7 @@
174
174
 
175
175
  ```Rails
176
176
 
177
- <% if message_id.notification.read = 'false' %>
178
-
179
- <%= unchecked_message_notifications_count %>
177
+ <%= unread_message_notifications_count %>
180
-
181
- <% end %>
182
178
 
183
179
  ```
184
180