评论属于已删除的用户。如何关联“占位符”?
我的评论是用户发表的。评论 belongs_to :user
和用户 has_many :comments
。
但用户可以被删除。如果完成,我宁愿不删除他们的评论,而是将他们的评论与一个虚拟用户相关联。
我可以想到几种方法:
- 在加载评论时,如果找不到关联的用户,则使用虚拟数据在内存中创建一个用户。
- 加载评论时,如果未找到关联用户,请从数据库中选择一个预定义用户。
- 删除评论时,将所有评论与数据库中预定义的用户相关联;通过一些后置过滤器。
我的感觉是,第一是最干净的;第二是最干净的。因为另外两个需要数据库中的用户,该用户将在代码中硬连线。如果用户 18394 是那个“特殊”用户,我需要为该特殊用户提供各种安全网。
My comments are placed by users. The Comment belongs_to :user
and the User has_many :comments
.
But users can be removed. If done, I'd rather not delete their comments, but instead associate their comments with one Dummy user.
I can think of several ways:
- On load of a comment, if no associated user is found, create a User in memory with dummy data.
- On load of a comment, if no associated user is found, pick a predefined one from the database.
- On removal of a comment, associate all the comments with a predefined User in the database; through some post filter.
My feeling says that number one is cleanest; since the other two require a user in the database that will be hardwired in the code. If user 18394 will be that "special" user, I'd need all kinds of safetynets for that special user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么软删除用户呢?有一个名为 User.active 的布尔字段,并为 User.active = t 设置默认范围。当用户被删除时,将活动字段设置为 false,并清除所有个人数据。
What about soft deleting users instead? Have a boolean field called User.active and set a default scope for User.active = t. When a user gets deleted, set the active field to false, and clear out any personal data.