您将如何在 RoR 中创建像 SO 或 Facebook 那样的通知系统?
我认为通知将是它自己的资源,并且通过代表关联的连接表与用户模型具有 has_many, through
关系。
一个用户有很多通知是显而易见的,那么一个通知就会有很多用户,因为会有很多标准化的通知(评论通知、关注通知等)与很多用户相关联。
除了此设置之外,我不确定如何根据应用程序中的某些事件触发通知的创建。我也有点不确定我需要如何设置路由 - 它是它自己的单独资源还是嵌套在用户资源中?如果有人可以对此进行扩展,我会发现它非常有帮助。
最后,ajax 轮询可能会改进这样的功能。
我可能缺少一些东西,所以请填写此内容,以便它成为一个很好的通用资源。
I'm thinking that notifications would be it's own resource and have a has_many, through
relationship with the user model with a join table representing the associations.
A user having many notifications is obvious, and then a notification would have many users because there would be a number of standardized notifications (a commenting notification, a following notification etc.) that would be associated with many users.
Beyond this setup, I'm unsure how to trigger the creation of notifications based on certain events in your application. I'm also a little unsure of how I'd need to set up routing - would it be it's own separate resource or nested in the user resource? I'd find it very helpful if someone could expand on this.
Lastly, ajax polling would likely improve such a feature.
There's probably some things I'm missing, so please fill this out so that it is a good general resource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以一般要点:
1)通知将是一个多态关联,因为评论可以有许多通知,用户可以有许多通知,“以下”可以有许多通知等。
2)你可以有 模型观察者,您可以在其中“观察”某些事件,例如何时创建新评论。这将是你的触发器。
在路由方面,您实际上不需要做任何不符合规范的事情。您可能拥有的唯一路由是domain.com/notifications,它显示所有通知。
通知表可能如下所示:
sender_id:整数,receiver_id:整数,notifying_id:整数,notifying_type:字符串
So the general gist:
1) Notifications would be a polymorphic association in that comments can have many notifications, users can have many notifications, a 'following' can have many notifications etc.
2) You can have Model Observers, where you can "observe" certain events, such as when a new comment is created. This is would be your triggers.
In terms of routing, you really don't need to do anything out of the norm. The only routing you may have is a domain.com/notifications where it shows all the notifications.
Notification table might look like:
sender_id: integer, receiver_id: integer, notifiable_id: integer, notifiable_type: string
对于通知系统我个人更喜欢服务器推送技术。 Ryan Bates(Railscasts 背后的声音)有一个精彩的屏幕演员,您可能会想要观看
要触发特定事件的操作,请查看@mike提到的“观察者”
For a notification system I personally prefer server push technology. Ryan Bates (the voice behind Railscasts) has a great screen cast that you might want to watch
For triggering actions for particular event, have a look at 'Observers' as @mike mentioned