2016-09-28 20:18:52 +08:00
|
|
|
class UserNotification < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :notification
|
2016-09-28 22:03:52 +08:00
|
|
|
|
2016-09-29 20:49:58 +08:00
|
|
|
def self.recent_notifications(user)
|
|
|
|
Notification.joins(:user_notifications)
|
|
|
|
.where('user_notifications.user_id = ?', user.id)
|
|
|
|
.order(created_at: :DESC)
|
|
|
|
.limit(10)
|
2016-09-28 22:03:52 +08:00
|
|
|
end
|
|
|
|
|
2016-10-03 14:20:23 +08:00
|
|
|
def self.unseen_notification_count(user)
|
2016-09-29 20:49:58 +08:00
|
|
|
where('user_id = ? AND checked = false', user.id).count
|
2016-09-28 22:03:52 +08:00
|
|
|
end
|
2016-10-04 02:31:00 +08:00
|
|
|
|
|
|
|
def self.seen_by_user(user)
|
|
|
|
where(user: user).map do |n|
|
|
|
|
n.checked = true
|
|
|
|
n.save
|
|
|
|
end
|
|
|
|
end
|
2016-09-28 20:18:52 +08:00
|
|
|
end
|