From 6452962f36262e30d73c123a14a06a8fb986aa4f Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 9 Aug 2013 12:12:56 -0400 Subject: [PATCH] FIX: Notification counters were being updated incorrectly. --- app/models/user.rb | 3 ++- .../20130809160751_fix_seen_notification_ids.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20130809160751_fix_seen_notification_ids.rb diff --git a/app/models/user.rb b/app/models/user.rb index f4f5d9f40fd..b350637af16 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -220,7 +220,8 @@ class User < ActiveRecord::Base end def saw_notification_id(notification_id) - User.where(["seen_notification_id < ?", notification_id]).update_all ["seen_notification_id = ?", notification_id] + User.where(["id = ? and seen_notification_id < ?", id, notification_id]) + .update_all ["seen_notification_id = ?", notification_id] end def publish_notifications_state diff --git a/db/migrate/20130809160751_fix_seen_notification_ids.rb b/db/migrate/20130809160751_fix_seen_notification_ids.rb new file mode 100644 index 00000000000..9a83d826051 --- /dev/null +++ b/db/migrate/20130809160751_fix_seen_notification_ids.rb @@ -0,0 +1,14 @@ +class FixSeenNotificationIds < ActiveRecord::Migration + def up + + # There was an error where `seen_notification_id` was being updated incorrectly. + # This tries to fix some of the bad data. + execute "UPDATE users SET + seen_notification_id = COALESCE((SELECT MAX(notifications.id) + FROM notifications + WHERE user_id = users.id AND created_at <= users.last_seen_at), 0)" + end + + def down + end +end