From bf957513ec60f1da7b7191747f0e39b168a93788 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 13 Feb 2014 17:27:35 +1100 Subject: [PATCH] correctly handle empty case --- app/models/notification.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index a8f3b5d4d67..0e11ba078f6 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -91,7 +91,7 @@ class Notification < ActiveRecord::Base def self.recent_report(user, count = nil) - notifications = user.notifications.recent(count).includes(:topic) + notifications = user.notifications.recent(count).includes(:topic).to_a if notifications.present? notifications += user.notifications @@ -99,17 +99,19 @@ class Notification < ActiveRecord::Base .where(read: false, notification_type: Notification.types[:private_message]) .where('id < ?', notifications.last.id) .limit(count) - end - notifications.sort do |x,y| - if x.unread_pm? && !y.unread_pm? - -1 - elsif y.unread_pm? && !x.unread_pm? - 1 - else - y.created_at <=> x.created_at - end - end.take(count) + notifications.sort do |x,y| + if x.unread_pm? && !y.unread_pm? + -1 + elsif y.unread_pm? && !x.unread_pm? + 1 + else + y.created_at <=> x.created_at + end + end.take(count) + else + [] + end end