Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Notifications rails中的用户通知_Notifications_Ruby On Rails 4 - Fatal编程技术网

Notifications rails中的用户通知

Notifications rails中的用户通知,notifications,ruby-on-rails-4,Notifications,Ruby On Rails 4,我有一个应用程序正在设计,其中包括用户交朋友和用户 有很多帖子,画,朋友和谈话 模型如下 class User < ActiveRecord::Base has_many :paintings, :dependent => :destroy has_many :sells, :dependent => :destroy has_many :posts, :dependent => :destroy has_many :talks, :dependent

我有一个应用程序正在设计,其中包括用户交朋友和用户 有很多帖子,画,朋友和谈话 模型如下

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
class用户:毁灭
有很多:销售,:依赖=>:销毁
有多个:post,:dependent=>:destroy
有很多:对话,:依赖=>:破坏
你有很多朋友吗
有很多评论
终止
我设置了一个通知系统,当用户创建帖子或绘画e.t.c 一个通知被发送给我通过公共_活动gem获得的用户朋友


我打算实现的是一个通知系统,在该系统中,当创建通知时,每个相关用户都可以标记为已看到,即他们已看到,这样通知就不会再显示给用户。。。而且,当用户对通知进行评论时,我希望将通知发送给活动的所有者,以及对该通知进行评论的任何其他用户。。。总之,我需要一个类似FACEBOOK的Notification系统…

在添加相关帖子/绘画后,您是否考虑过创建通知

将创建通知(可能在每个相关模型上使用创建后操作,例如帖子、绘画)

class Post

如果要创建很多通知,那么您可能希望考虑在后台作业中创建它们。

通知将属于用户的目标朋友,因此可以标记为通过通知上的属性查看。您可以在通知对象上包含实例方法来处理此问题

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

 def mark_seen
   update_attributes(viewed: true)
 end

end
类通知
您还可以在通知上添加作用域,以确保用户可以轻松地看到未看到的通知

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

希望这有助于让您走上正轨。

谢谢您的快速回复,我希望您能帮我解决这一问题。谢谢您的快速回复,我希望您能帮我解决这一问题。评论通知也会显示给活动的所有者和发表评论的用户,就像我们对Post模型所做的一样,您也可以为注释模型添加after_create操作,以便在保存注释后始终创建通知。事实上,您可以为每个需要通知的模型执行此操作(您甚至可以更高级,将每个需要通知的模型的“创建后”操作提取到模型关注点中!)这将有助于保持代码整洁。它只会将通知发送给所有朋友,而不是仅向对活动发表评论的用户。让我向您展示我的一些代码@activities=activity。其中(user\u id:[当前用户.朋友id,当前用户])。分页(:page=>params[:page],:per\u page=>10)这只负责向用户和朋友显示活动。如果我想向用户展示活动,并且只有用户对活动发表评论,我认为最好的办法是确保每个活动都有正确的用户id,无论是当前用户、他们的朋友还是评论者。然后,您可以在Activity上创建一个类方法,以进一步细化返回的Activity(基于注释的用户id)
class User < ActiveRecord::Base
  has_many :notifications
  has_many :unseen_notifications, conditions: "notifications.viewed IS false" #or something like that
end