From 418548b9baea2bfe5b46e4ea771e52c41fcff010 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 3 Nov 2025 16:51:19 +0100 Subject: [PATCH] More efficient query for watched_by? and use etag for the status --- app/controllers/cards/watches_controller.rb | 4 ++++ app/models/card/watchable.rb | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/cards/watches_controller.rb b/app/controllers/cards/watches_controller.rb index 7308c2279..e1c181335 100644 --- a/app/controllers/cards/watches_controller.rb +++ b/app/controllers/cards/watches_controller.rb @@ -1,6 +1,10 @@ class Cards::WatchesController < ApplicationController include CardScoped + def show + fresh_when etag: @card.watch_for(Current.user) + end + def create @card.watch_by Current.user end diff --git a/app/models/card/watchable.rb b/app/models/card/watchable.rb index 2d23868bf..17235529e 100644 --- a/app/models/card/watchable.rb +++ b/app/models/card/watchable.rb @@ -9,7 +9,11 @@ module Card::Watchable end def watched_by?(user) - watchers.include?(user) + watch_for(user)&.watching? + end + + def watch_for(user) + watches.find_by(user: user) end def watch_by(user)