问题描述:

i have an application am designing which consists of users making friends and users

having many posts, paintings, friends and talks

the model is below

class User < ActiveRecord::Base

has_many :paintings, :dependent => :destroy

has_many :sells, :dependent => :destroy

has_many :posts, :dependent => :destroy

has_many :talks, :dependent => :destroy

has_many :friends

has_many :comments

end

i have set a notification system in which when a user creates a post or painting e.t.c

a notification is sent to the users friends which i have achieved with public_activity gem,

what i intend to achieve is a notification system in which when the notification is created, every user involved can mark as seen i.e they have seen it so THAT notification is not shown to the user anymore... And also when a user comments on a notification, i want a notification to be sent to the owner of the activity, and any other user that comments on THAT notification... IN SUMMARY, I NEED A FACEBOOK LIKE NOTIFACATION SYSTEM...

网友答案:

Have you considered creating a Notification after the relevant post/painting has been added?

Notifications would be created (probably using an after_create action on each of the relevant models e.g. posts, paintings).

class Post < ActiveRecord::Base
 after_create :create_notification

 private

  def create_notification
    # create notification here
  end
end

If there are lots of notifications to be created then you may wish to consider creating them in a background job.

A notification would belong to the target friend of the user and could therefore be marked as seen through an attribute on the notification. You would include instance methods on the notification object to deal with this.

class Notification < ActiveRecord::Base
 belongs_to :user # friend of the originating user

 def mark_seen
   update_attributes(viewed: true)
 end

end

You could also add scopes on the notifications to make sure that users can easily see unseen notifications.

class User < ActiveRecord::Base
  has_many :notifications
  has_many :unseen_notifications, conditions: "notifications.viewed IS false" #or something like that
end

Hope this helps to set you on the right track.

相关阅读:
Top