I use the public_activity gem to create a notification list for users. Using this post as a link, I try to save the number of notifications that the user has not read. When the user clicks to see the notifications that they have, I want the account to return to zero. The solution to the above question is to create a class method as follows:
def self.unread
where(:read => false)
end
and then put this in the view:
user.notifications.unread.update_all(:read => true)
My controller is as follows:
def notifications
@activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user.id)
end
def self.unread
where(:read => false)
end
and my view is as follows:
<% @activities.each do |activity| %>
<%= render_activity activity %>
<% end %>
My question is: where to add:
.update_all(:read => true)
in the view and how can I get unread.count.
source
share