I have User
model which has_many :notifications
. Notification
has a boolean column seen
and a scope called :unseen
which returns all notifications where seen
is false
.
class User < ApplicationRecord
has_many :notifications
has_many :unseen_notifications, -> { unseen }, class_name: "Notification"
end
I know that I can cache the number of notifications if I add a column called notifications_count
to users
and add counter_cache: true
to my belongs_to
call in Notification
.
But what if I want to cache the number of unseen notifications a user has? I.e. cache unseen_notifications.size
instead of notifications.size
? Is there a built-in way to do this with counter_cache
or do I have to roll my own solution?