问题描述
我在一个文学社区网站上工作.(截图)我正在尝试弄清楚当有人对他们发布到网站的内容发表评论时,当他们正在观看的人提交新的文学作品等时,如何通知用户.
I'm working on a literature community website. (screenshot) And I'm trying to figure out how to notify users when someone comments on something they posted to the site, when someone they are watching submissions a new literature peice, etc.
我想弄清楚如何构建数据库来存储这些信息.我想出了两个可能的想法.
I'm trying to figure out how to structure the database to store this information. I've come up with two possible ideas.
存储指向可通知对象的链接,该字段描述了用户被通知的操作类型(新建、更新等).这会产生复杂的显示代码,但这意味着我可以相当轻松地更改通知的工作方式.这也会增加我需要从数据库中提取的数据,除非我使用缓存字段将相关属性的散列转储到表中.
Store a link to the notifiable object, a field describing the type of action (new, update, etc) that the user is being notified of. This makes for complex display code but it means I can change how notifications work rather easily. This also increase the data I need to pull from the database unless I use a cache field to dump a hash of relevant attributes into the table.
- notifiable_type
- notifiable_id
- user_id
- 行动
- notifiable_cache(可选,存储来自通知对象的选定属性的哈希值)
将通知视为电子邮件,只需将它们与主题和消息一起保存到数据库中.这导致一个简单的视图但一个复杂的模型,并阻止我轻松更改通知的工作方式.
Treat the notifications like email and just save them to the database with a subject and message. This results in a simple view but a complex model and prevents me from easily changing how notifications work.
- user_id
- 标题
- 留言
我正在寻找关于我上面列出的两个的其他想法和评论.
I'm looking for other ideas and comments on the two I listed above.
推荐答案
我也在做一个使用通知的项目,我不确定你现在是否已经解决了你的问题,或者它是否有帮助,但这是我使用的表结构:
I'm working on a project utilizing notifications as well, I'm not sure if you got yours sorted out by now or if it might help but this is the table structure I used:
Notifications:
- ID (PK)
- recipient_id
- sender_id
- activity_type ('comment on a post', 'sent friend request', etc)
- object_type ('post', 'photo', etc)
- object_url (to provide a direct link to the object of the notification in HTML)
- time_sent
- is_unread
这篇关于将通知存储给用户的数据库设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!