まず、ロジックが異なるので個人用の通知とは別のテーブルで管理したほうが良いと思います
またユーザ一人に対して一つ通知を作成する方法は懸念の通りデータ量の問題もありますが、通知の削除や編集なのも不便なので、既読データは別テーブルにするのが良いでしょう
更にデータ量削減を優先するなら未読データを作らない手もあります
ruby
1class User < ApplicationRecord
2 has_many :information_reads
3 has_many :read_infomations, through: :information_reads, class_name: 'Information'
4
5 # 未読データを作らないので既読の否定で算出する
6 scope :unread_infomations, (lamda do
7 Information.where.not(id: information_readings.select(:information_id))
8 end)
9end
10
11# 全ユーザー分の未読データを作るのが無駄なので既読データだけを登録する
12class InfomationRead < ApplicationRecord
13 belongs_to :user
14 belongs_to :information
15end
16
17# 通知データ本体はユーザー別には作らない
18class Information < ApplicationRecord
19 # 通知削除時には既読データを一緒に消す
20 has_many :infomation_read, dependent: :delete_all
21end
■ 補足
未読データを作らない場合、通知作成後の新規ユーザーにも過去通知が未読で見えることになります
そのような事を避けたい場合は、日付など何らかの方法で制御する必要があるので注意が必要です
ruby
1 scope :unread_infomations, (lamda do
2 Information.where.not(id: information_readings.select(:information_id))
3 .where('users.created_at < informations.created_at')
4 end)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/19 03:04