インターン(53) ブロック機能のバグ修正

何日目か考えるのが面倒なので単なる連番にする.

前回の作業週から取り組んでいたブロック機能のバグ修正の話. ブロックしたユーザが非表示にならず, 再度ブロックするとDBのユニーク制約に違反してエラーになるのが原因だった.

なのでブロック済みユーザのコメントは取得しないようにしようと試みる. 怖話では, 怖い話(Story), 怖い画像(Wallpaper), 怖い漫画(Comic)のそれぞれにコメントを付けることが出来(acts_as_commentable), ブロックは投稿したユーザからコメントしたユーザへの一方向になっている. また, 管理者以外のユーザがブロック済みユーザのコメントを見ることは無いはずなのでCommentクラスのdefault_scopeに

default_scope -> {
  joins('left join stories on comments.commentable_type = "Story" and comments.commentable_id = stories.id ').
  joins('left join wallpapers on comments.commentable_type = "Wallpaper" and comments.commentable_id = wallpapers.id ').
  joins('left join comics on comments.commentable_type = "Comic" and comments.commentable_id = comics.id ').
  where('comments.user_id not in (select blocks.destination_id from blocks where blocks.source_id =
    (case when comments.commentable_type = "Story" then stories.user_id
          when comments.commentable_type = "Wallpaper" then wallpapers.user_id
          else comics.user_id end))')
}

と書いた.

これで”ブロック済みユーザのコメントは取得しない”という目的は達成されるのだが, 今度はincludes(:commentable)とするとActiveRecord::EagerLoadPolymorphicError - Can not eagerly load the polymorphic association :commentableとなってしまう.

回避する方法は分からなかったのだが, 現状includes(:commentable)が必要になる箇所はトップページ(/), コメント一覧ページ(/comments), 怖い漫画のトップページ(/comics)であり, 特にトップページは表示回数が多いはずなのでRailsやその他のキャッシュで対処出来ないかな…という淡い期待でそのままにした.

何か他にいい方法無いかな…