Open2

投稿が削除できなくなった

さいれずさいれず

画像投稿機能を実装してから投稿が削除できなくなってしまった。

Rails Cコマンドで投稿を削除できるかを検証したところ、いくつかわかったことがあるのでメモしておく。
まず、Post.destroy(id)で対象の投稿が削除できるかを検証

irb(main):004:0> Post.destroy(10)
  Post Load (9.1ms)  SELECT  `posts`.* FROM `posts` WHERE `posts`.`id` = 10 LIMIT 1
   (0.6ms)  BEGIN
  Like Load (0.9ms)  SELECT `likes`.* FROM `likes` WHERE `likes`.`post_id` = 10
  Comment Load (1.2ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 10
  Notification Load (0.9ms)  SELECT `notifications`.* FROM `notifications` WHERE `notifications`.`post_id` = 10 ORDER BY `notifications`.`created_at` DESC
  Image Load (0.9ms)  SELECT `images`.* FROM `images` WHERE `images`.`post_id` = 10
  Post Destroy (0.8ms)  DELETE FROM `posts` WHERE `posts`.`id` = 10
  User Load (0.8ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Post Load (0.7ms)  SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1
  Like Load (0.7ms)  SELECT `likes`.* FROM `likes` WHERE `likes`.`post_id` = 1
  Comment Load (1.0ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 1
  Notification Load (0.8ms)  SELECT `notifications`.* FROM `notifications` WHERE `notifications`.`post_id` = 1 ORDER BY `notifications`.`created_at` DESC
  Image Load (0.6ms)  SELECT `images`.* FROM `images` WHERE `images`.`post_id` = 1
  Image Destroy (0.6ms)  DELETE FROM `images` WHERE `images`.`id` = 82
   (2.3ms)  ROLLBACK

という結果が返ってきた。

画像やタグの部分に問題があると考えて、一度DBをリセットし、タグと画像投稿機能を使えない状態にして、投稿を削除できるか検証した。

irb(main):002:0> Post.destroy(1)
  Post Load (1.1ms)  SELECT  `posts`.* FROM `posts` WHERE `posts`.`id` = 1 LIMIT 1
  Like Load (0.7ms)  SELECT `likes`.* FROM `likes` WHERE `likes`.`post_id` = 1
  Comment Load (0.4ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 1
  Notification Load (1.0ms)  SELECT `notifications`.* FROM `notifications` WHERE `notifications`.`post_id` = 1 ORDER BY `notifications`.`created_at` DESC
  Image Load (0.6ms)  SELECT `images`.* FROM `images` WHERE `images`.`post_id` = 1
  Post Destroy (1.9ms)  DELETE FROM `posts` WHERE `posts`.`id` = 1
  User Load (0.8ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  Post Load (0.6ms)  SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 2
  Like Load (1.4ms)  SELECT `likes`.* FROM `likes` WHERE `likes`.`user_id` = 2
  Comment Load (1.2ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 2
  Notification Load (0.7ms)  SELECT `notifications`.* FROM `notifications` WHERE `notifications`.`visitor_id` = 2 ORDER BY `notifications`.`created_at` DESC
  Notification Load (0.8ms)  SELECT `notifications`.* FROM `notifications` WHERE `notifications`.`visited_id` = 2 ORDER BY `notifications`.`created_at` DESC
  Message Load (0.8ms)  SELECT `messages`.* FROM `messages` WHERE `messages`.`user_id` = 2
  Entry Load (0.8ms)  SELECT `entries`.* FROM `entries` WHERE `entries`.`user_id` = 2
  Relationship Load (0.9ms)  SELECT `relationships`.* FROM `relationships` WHERE `relationships`.`user_id` = 2
  Relationship Load (2.1ms)  SELECT `relationships`.* FROM `relationships` WHERE `relationships`.`follow_id` = 2
  User Destroy (0.8ms)  DELETE FROM `users` WHERE `users`.`id` = 2
   (2.3ms)  COMMIT
=> #<Post id: 1, body: "aaaa", visit_day: "2021-03-12", created_at: "2021-03-12 13:56:35", updated_at: "2021-03-12 13:56:35", user_id: 2, images: nil>

すると見事削除できた!投稿したユーザーも一緒に!!!致命的すぎるでしょうよ…
この原因はめちゃ簡単で、post.rbで

belongs_to :user, dependent: :destroy

としていたのを

belongs_to :user

とすることで解決した。まじで頭悪いミスで笑けてくる。

ユーザーと紐づいた状態なので、投稿を削除した際に関連しているユーザーも削除するという処理になってしまっていた。

  def destroy
    # Rails Cコマンドで投稿を削除できるかを検証したところ、
    @post.destroy
    redirect_to profile_user_url(@post.user.id), notice: "投稿を削除しました" 
    # redirect_to posts_url, notice: "投稿を削除しました" 
  end
さいれずさいれず

上記の問題だけど、

[application.html.slim]
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'

という記述がVue.jsを導入しているので、turbolinksと衝突して悪さをしてたっぽい。

[application.html.slim]
= javascript_pack_tag 'application', name: 'turbolinks-cache-control', content: 'no-cache'

という風に記述したらしっかりと動くようになった。

ちなみにその後に、Vue-tuboslinksを入れたら上のやつだと動かなくなったので、

[application.html.slim]
= javascript_include_tag 'application'

という記述をしたら無事に動くようになった。

多分だけど、Vue-tuboslinksのおかげでVueとtuboslinksの関係性が良くなったのでは?と思ってる。ようわからんな…