質問編集履歴

4

post.rbの中身を変更。

2020/11/30 05:45

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -364,6 +364,36 @@
364
364
 
365
365
  ```
366
366
 
367
+ class Post < ApplicationRecord
368
+
369
+ validates :title, {presence: true, length: {maximum: 50}}
370
+
371
+ # validates :level, {presence: true}
372
+
373
+ # validates :maximum, {presence: true}
374
+
375
+ # validates :message, {presence: true, length: {maximum: 140}}
376
+
377
+ # validates :user_id, {presence: true}
378
+
379
+
380
+
381
+ belongs_to :user
382
+
383
+ has_many :likes, dependent: :destroy
384
+
385
+ has_many :comments, dependent: :destroy
386
+
387
+
388
+
389
+ def user
390
+
391
+ return User.find_by(id: self.user_id)
392
+
393
+ end
394
+
395
+
396
+
367
397
  has_many :notifications, dependent: :destroy
368
398
 
369
399
 
@@ -396,6 +426,54 @@
396
426
 
397
427
  end
398
428
 
429
+
430
+
431
+ def create_notification_comment!(current_user, comment_id)
432
+
433
+ temp_ids = Comment.select(:user_id).where(post_id: id).where.not(user_id: current_user.id).distinct
434
+
435
+ temp_ids.each do |temp_id|
436
+
437
+ save_notification_comment!(current_user, comment_id, temp_id['user_id'])
438
+
439
+ end
440
+
441
+ save_notification_comment!(current_user, comment_id, user_id) if temp_ids.blank?
442
+
443
+ end
444
+
445
+
446
+
447
+ def save_notification_comment!(current_user, comment_id, visited_id)
448
+
449
+ notification = current_user.active_notifications.new(
450
+
451
+ post_id: id,
452
+
453
+ comment_id: comment_id,
454
+
455
+ visited_id: visited_id,
456
+
457
+ action: 'comment'
458
+
459
+ )
460
+
461
+ return if notification.visited_id == notification.visitor_id
462
+
463
+ if notification.visitor_id == notification.visited_id
464
+
465
+ notification.checked = true
466
+
467
+ end
468
+
469
+ notification.save if notification.valid?
470
+
471
+ end
472
+
473
+ end
474
+
475
+
476
+
399
477
  ```
400
478
 
401
479
 

3

「【追記2】ストロングパラメータ変更後」を追記。

2020/11/30 05:45

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -431,3 +431,105 @@
431
431
  [29] pry(main)>
432
432
 
433
433
  ```
434
+
435
+
436
+
437
+ ###【追記2】ストロングパラメータ変更後
438
+
439
+
440
+
441
+ `comments.controller.rb`内のストロングパラメータを変更しました。
442
+
443
+ ```Ruby
444
+
445
+ @comment = Comment.new(comment_params)
446
+
447
+ @post = @comment.post
448
+
449
+
450
+
451
+ Comment.create(body: comment_params[:body], post_id: params[:post_id], user_id: @current_user.id)
452
+
453
+ if @comment.save!
454
+
455
+ @post.create_notification_comment!(@current_user, @comment.id)
456
+
457
+ redirect_back(fallback_location: post_comments_path) #save出来ても出来なくても、以下のルートにリダイレクト。
458
+
459
+ else
460
+
461
+ redirect_back(fallback_location: post_comments_path)
462
+
463
+ end
464
+
465
+ end
466
+
467
+
468
+
469
+ def comment_params
470
+
471
+ params.require(:comment).permit(:body)
472
+
473
+ end
474
+
475
+
476
+
477
+ ↓ # comment_paramsの中身のみ変更。
478
+
479
+
480
+
481
+ def comment_params
482
+
483
+ params.require(:comment).permit(:body).merge(post_id: params[:post_id], user_id: @current_user.id)
484
+
485
+ end
486
+
487
+ ```
488
+
489
+
490
+
491
+ するとコメントを送信する度に2重に保存されるようになった(つまり`comment_params`が機能する様になった)ので、以下の1行を消す事でコメントが通常通り保存されるようになりました。
492
+
493
+ ```
494
+
495
+ Comment.create(body: comment_params[:body], post_id: params[:post_id], user_id: @current_user.id)
496
+
497
+ ```
498
+
499
+
500
+
501
+ それでも通知テーブルにコメントのデータは保存されず、コマンドにて以下のエラーが出ました。
502
+
503
+ ```
504
+
505
+ NoMethodError (undefined method `create_notification_comment!' for #<Post:0x000000000f0c16b0>
506
+
507
+ Did you mean? create_notification_like!):
508
+
509
+ ```
510
+
511
+
512
+
513
+ どうやら`create_notification_comment!`は`post.rb`内に記述しないといけない(?)ようだったので、定義を`comment.rb`から`post.rb`に移行させました。これにより、`post.rb`内での`comment`に関する記述は以下の一行のみになりました。
514
+
515
+ ```
516
+
517
+ has_many :notifications, dependent: :destroy
518
+
519
+ ```
520
+
521
+
522
+
523
+ するとようやく、コメントを送信後、通知テーブルにそのデータが保存されるようになりました。またビューの`notifications/index`でもしっかり通知が呼び出されました。
524
+
525
+
526
+
527
+ しかしあと2点だけ問題が出てきました。
528
+
529
+ 1. コメントを削除しても通知のデータが残ったままになる(`dependent: :destroy`)が機能しない。
530
+
531
+ 2. ログイン中のユーザーのコメントも、本人の通知に表示されてしまう。
532
+
533
+
534
+
535
+ 2に関して、`action`は`true`で保存されているので、保存のされ方は問題ないかと思います。後はビュー内でも、本人には通知がいかないようにする旨の記述を加える必要があると言う事でしょうか?

2

Commentテーブルのデータベースの中身を追記。

2020/11/29 11:51

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -397,3 +397,37 @@
397
397
  end
398
398
 
399
399
  ```
400
+
401
+
402
+
403
+ ### Commentテーブルのデータベースの中身
404
+
405
+ ```
406
+
407
+ [27] pry(main)> Comment.all
408
+
409
+ Comment Load (0.4ms) SELECT "comments".* FROM "comments"
410
+
411
+ +----+---------+---------+------+-------------------------+-------------------------+
412
+
413
+ | id | user_id | post_id | body | created_at | updated_at |
414
+
415
+ +----+---------+---------+------+-------------------------+-------------------------+
416
+
417
+ | 1 | 23 | 2 | hoge | 2020-11-29 09:09:27 UTC | 2020-11-29 09:09:27 UTC |
418
+
419
+ | 2 | 23 | 2 | huga | 2020-11-29 09:10:10 UTC | 2020-11-29 09:10:10 UTC |
420
+
421
+ +----+---------+---------+------+-------------------------+-------------------------+
422
+
423
+ 2 rows in set
424
+
425
+ [28] pry(main)> Comment.last(2).pluck(:body)
426
+
427
+ Comment Load (0.6ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" DESC LIMIT ? [["LIMIT", 2]]
428
+
429
+ => ["hoge", "huga"]
430
+
431
+ [29] pry(main)>
432
+
433
+ ```

1

post.rbの追記。

2020/11/29 09:12

投稿

punchan36
punchan36

スコア105

test CHANGED
File without changes
test CHANGED
@@ -357,3 +357,43 @@
357
357
  RubyGems 3.0.3
358
358
 
359
359
  Rails 5.2.3
360
+
361
+
362
+
363
+ ### 追記 post.rb
364
+
365
+ ```
366
+
367
+ has_many :notifications, dependent: :destroy
368
+
369
+
370
+
371
+ def create_notification_like!(current_user)
372
+
373
+ temp = Notification.where(["visitor_id = ? and visited_id = ? and post_id = ? and action = ? ", current_user.id, user_id, id, 'like'])
374
+
375
+ if temp.blank?
376
+
377
+ notification = current_user.active_notifications.new(
378
+
379
+ post_id: id,
380
+
381
+ visited_id: user_id,
382
+
383
+ action: 'like'
384
+
385
+ )
386
+
387
+ if notification.visitor_id == notification.visited_id
388
+
389
+ notification.checked = true
390
+
391
+ end
392
+
393
+ notification.save if notification.valid?
394
+
395
+ end
396
+
397
+ end
398
+
399
+ ```